r - How to use the output of of paste function in rbind.fill? -
i have list of dataframe trying rbind.fill
don't have same number of columns. dataframes names x1,x2,...x10.
my code :
x.list<-list(c(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)) library(plyr) rbind.fill(x.list)
this code works, trying avoid writing dataframe using paste0
, i.e.
x.list1<-as.list(paste0(x,1:10))
x.list1 interprets x1, x2,... characters rather dataframe:
str(x.list1) list of 10 $ : chr "x1" $ : chr "x2" $ : chr "x3" $ : chr "x4" $ : chr "x5" $ : chr "x6" $ : chr "x7" $ : chr "x8" $ : chr "x9" $ : chr "x10"
so, can't use rbind.fill
since expects list of dataframes. have tried using mget
suggested here
rbind.fill(mget(x.list1))
but, got error,
error in mget(x.list1) : argument "envir" missing, no default
setting environment (as mentioned in comment of answer earlier question) doesn't either:
rbind.fill(mget(x.list1,envir = .globalenv)) error in mget(x.list1, envir = .globalenv) : invalid first argument
any suggestion fix issue?
here sample dataframes x1,x2,and x3:
x1<-structure(list(mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2), cyl = c(6, 6, 4, 6, 8, 6, 8, 4, 4, 6), disp = c(160, 160, 108, 258, 360, 225, 360, 146.7, 140.8, 167.6), hp = c(110, 110, 93, 110, 175, 105, 245, 62, 95, 123)), .names = c("mpg", "cyl", "disp", "hp"), row.names = c("mazda rx4", "mazda rx4 wag", "datsun 710", "hornet 4 drive", "hornet sportabout", "valiant", "duster 360", "merc 240d", "merc 230", "merc 280"), class = "data.frame") x2<-structure(list(mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2), cyl = c(6, 6, 4, 6, 8, 6, 8, 4, 4, 6), disp = c(160, 160, 108, 258, 360, 225, 360, 146.7, 140.8, 167.6), hp = c(110, 110, 93, 110, 175, 105, 245, 62, 95, 123), drat = c(3.9, 3.9, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92), wt = c(2.62, 2.875, 2.32, 3.215, 3.44, 3.46, 3.57, 3.19, 3.15, 3.44)), .names = c("mpg", "cyl", "disp", "hp", "drat", "wt"), row.names = c("mazda rx4", "mazda rx4 wag", "datsun 710", "hornet 4 drive", "hornet sportabout", "valiant", "duster 360", "merc 240d", "merc 230", "merc 280"), class = "data.frame") x3<-structure(list(mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8), cyl = c(6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6), disp = c(160, 160, 108, 258, 360, 225, 360, 146.7, 140.8, 167.6, 167.6)), .names = c("mpg", "cyl", "disp"), row.names = c("mazda rx4", "mazda rx4 wag", "datsun 710", "hornet 4 drive", "hornet sportabout", "valiant", "duster 360", "merc 240d", "merc 230", "merc 280", "merc 280c"), class = "data.frame")
use lapply
anonymous function , get
.
# use: lapply(paste0("x", 1:10), function(x) get(x)) # instead of: as.list(paste0(x,1:10))
Comments
Post a Comment