for loop - R 3.1.0 number of items to replace is not a multiple of replacement length error -


i have written following r code:

csv_data<-temp[1:3]  req_data<-null  for(j in 1:length(1:3)){   req_data[j]<-read.table(csv_data,sep=",") } 

csv_data contains 3 files "001.csv", "002.csv" , "003.csv"

each file has 4 columns

after loop expect req_data contain 3 files as:

req_data[1] - contains "001.csv" (all 4 columns) req_data[2] - contains "002.csv" (all 4 columns) req_data[3] - contains "003.csv" (all 4 columns) 

but req_data contains:

req_data[1] - contains "001.csv" (only 1st column) req_data[2] - contains "002.csv" (only 1st column) req_data[3] - contains "003.csv" (only 1st column) 

how can expected result?

i doubt program works without giving 3 warnings. since read.table returns data.frame, can make self-contained example als follows.

req_data<-null  for(j in 1:length(1:3)){   req_data[j]<- iris[sample(nrow(iris),10),] } 

giving: warning messages: 1: in req_data[j] <- iris[sample(nrow(iris), 10), ] : number of items replace not multiple of replacement length

when write:

req_data[[j]] <-... 

you adding dataframe elements list, should work required. probable need do.call(rbind- magic afterward, not part of question.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -