r - mapply to add unique columnnames in a list -


say have list:

thelist=list(first=data.frame(year=1:10,time=rnorm(10)),second=data.frame(year=1:10,time=rnorm(10)),third=data.frame(year=1:10,time=rnorm(10))) 

i want add column each list element, names column differ.

test=mapply(function(x,y) {       x$y=x$time+0.5       x=x[complete.cases(x[,3]),][,c(1,3)]                                  },       x=thelist,y=c("add1","add2","add3")           ) 

and answer

test      first      second     third      year integer,10 integer,10 integer,10 y    numeric,10 numeric,10 numeric,10 

it not expect. answer be:

test[1:2]  $first    year  time  add1  1     1 -0.27  values... 2     2  0.76 3     3  1.53 4     4  1.00 5     5 -0.25 6     6  0.64 7     7 -0.38 8     8  1.52 9     9 -1.18 10   10 -0.97  $second    year   time  add2 1     1  0.330  values... 2     2  0.075 3     3  1.357 4     4 -1.393 5     5 -0.382 6     6 -0.016 7     7  0.604 8     8 -0.721 9     9  0.665 10   10 -1.115 

update:

if use simplify=false

test=mapply(function(x,y) {    x$y=x$time+0.5    x=x[complete.cases(x[,3]),][,c(1,3)]       },              x=thelist,y=c("add1","add2","add3"),simplify=false   )  test[1:2] $first    year     y 1     1  0.23 2     2  1.26 3     3  2.03 4     4  1.50 5     5  0.25 6     6  1.14 7     7  0.12 8     8  2.02 9     9 -0.68 10   10 -0.47  $second    year     y 1     1  0.83 2     2  0.57 3     3  1.86 4     4 -0.89 5     5  0.12 6     6  0.48 7     7  1.10 8     8 -0.22 9     9  1.16 10   10 -0.62 

try this:

test <- mapply(function(x,y) {     x[[y]] <- x$time + 0.5     x <- x[complete.cases(x[,3]), c(1,3)]     },    thelist, c("add1","add2","add3"), simplify = false) 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -