Question

Define a list dats with two dataframes, df1 and df2

dats <- list( df1 = data.frame(a=sample(1:3), b = sample(11:13)),
    df2 = data.frame(a=sample(1:3), b = sample(11:13)))

> dats
$df1
  a  b
1 2 12
2 3 11
3 1 13

$df2
  a  b
1 3 13
2 2 11
3 1 12

I would like to drop variable a in each data frame. Next I would like to add a variable with the id of each dataframe from an external dataframe, like:

ids <- data.frame(id=c("id1","id2"),df=c("df1","df2"))
> ids
  id  df
1 id1 df1
2 id2 df2

To drop unnecessary vars I tried this without luck:

> dats <- lapply(dats, function(x) assign(x, x[,c("b")]))  
> Error in assign(x, x[, c("b")]) : invalid first argument

Not sure how to add the id either.

I also tried, perhaps more appropriately:

> temp <- lapply(dats, function(x) subset(x[1], select=x[[1]]$b))
Error in x[[1]]$b : $ operator is invalid for atomic vectors

What I find confusing is that str(out[1]) returns a list, str(out[[1]]) returns a dataframe. I think that may have something to do with it.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top