r - Splitting a dataframe by column name indices -
this variation of earlier question.
df <- data.frame(matrix(rnorm(9*9), ncol=9)) names(df) <- c("c_1", "d_1", "e_1", "a_p", "b_p", "c_p", "1_o1", "2_o1", "3_o1")
i want split dataframe index given in column.names after underscore "_". (the indices can character/number in different lengths; these random examples).
indx <- gsub(".*_", "", names(df))
and name resulting dataframes accordingly n end 3 dataframes, called:
- df_1
- df_p
- df_o1
thank you!
here, can split column names indx
, subset of data within list using lapply
, [
, set names of list elements using setnames
, , use list2env
if need them individual datasets (not recommended of operations can done within list , later if want, can saved using write.table
lapply
.
list2env( setnames( lapply(split(colnames(df), indx), function(x) df[x]), paste('df', sort(unique(indx)), sep="_")), envir=.globalenv) head(df_1,2) # c_1 d_1 e_1 #1 1.0085829 -0.7219199 0.3502958 #2 -0.9069805 -0.7043354 -1.1974415 head(df_o1,2) # 1_o1 2_o1 3_o1 #1 0.7924930 0.434396 1.7388130 #2 0.9202404 -2.079311 -0.6567794 head(df_p,2) # a_p b_p c_p #1 -0.12392272 -1.183582 0.8176486 #2 0.06330595 -0.659597 -0.6350215
or using map
. similar above approach ie. split column names indx
, use [
extract columns, , rest above.
list2env(setnames(map(`[` , list(df), split(colnames(df), indx)), paste('df',unique(sort(indx)), sep="_")), envir=.globalenv)
update
you can do:
indx1 <- factor(indx, levels=unique(indx)) split(colnames(df), indx1)
Comments
Post a Comment