r - split column if variable number of pieces data.frame -
i want split column y
of df
below according '_' data incomplet. (df
representative portion of bigger data.frame
).
df <- data.frame(x = 1:10, y = c("vuh_ftu_yefq", "sos_nvtspb", "pfymm_ucms", "tucbexcqzh", "n_zndbhoun", "wdetzaolvn", "lvohrpdqns", "wso_bsqwvr", "wx_gbkbxjl", "t_dbxkkvge"))
i have tried using:
df$z <- strsplit(df$y,'_')
but error because number of pieces in each list different.
how can this?
assumptions:
)
needed close out df in example.incomplete data
means it's filled in left such value without intervening '_' first or datum.
tidyr
's separate()
:
result <- separate(df, y, = c("z1","z2","z3") , sep ='_', = "drop")
- the key here
extra = "drop"
according docs always returns length(into) pieces dropping or expanding necessary.
data.table
's tstrsplit()
dt <- as.data.table(df) result <- dt[, c("z1", "z2","z3") := tstrsplit(y, '_', fixed=true)][]
- the default behaviour
tstrsplit()
need ,fixed=true
passstrsplit()
underneath keep things hasty.
note: if incomplete data filled right need unmix variables here!!!
Comments
Post a Comment