create a multiple data frames from a single data frame in r -
i've got large data frame finaldata
, produce bunch of other smaller data frames explanatory1, explanatory2 e.t.c....
consisting of 10 columns each finaldata
i'm trying using loop throwing me error attempt apply non function
for(i in 1:length(finaldata)/10) { nam <- paste("explanatory", i, sep = "") assign(nam, finaldata[,10(i):10(i)+10]) }
i have tried
for(i in 1:length(finaldata)/10){ assign(paste("explanatory",i,sep=""),finaldata[,10(i):10(i)+10])}
but gave me same error, understand error being caused passing finaldata[,10(i):10(i)+10]
argument assign, don't see why wouldn't work ina loop, or different passing finaldata[,10:10+10]
any appreciated!
create sample data play with:
df <- data.frame(matrix(vector(), 10, 33))
find number of dataframes you're going create:
number_of_dataframes <- ceiling(ncol(df) / 10)
loop through dataframes, finding range of columns use creating individual dataframe. use assign
give each 1 unique name:
current_column <- 1 (i in 1:number_of_dataframes) { start_column <- current_column end_column <- min(current_column + 9, ncol(df)) assign(paste0("df",i), df[ , start_column:end_column]) current_column <- end_column + 1 }
the min
check makes sure don't attempt assign more columns existed in original dataframe.
Comments
Post a Comment