Reputation: 115
this is presumably a simple question, but I have been unable to find an answer. I want to delete a column of a dataframe that is inside a list.
x1<- rnorm(100,0,1)
x2<- rnorm(100,0,1)
x3<- rnorm(100,0,1)
x4 <- rnorm(100,0,1)
df1 <- data.frame(x1,x2,x3)
df2 <- data.frame(x4)
l1 <- list(df1,df2)
l1[1]
data.frame(l1[1])[,-1]
l1[1] <- data.frame(l1[1])[,-1]
Consider this example in which two dataframes, df1 and df2, are in list, l1. I want to delete column x1 out of df1. This is trivial to do if this is just a dataframe. But once inside a list, I am not sure how to manipulate this dataframe. When I try to overwrite it in the last statement, I am getting an error.
My actual problem has about 100 dataframes in a list and about 10% of them have an additional column that I need to delete. I can easily identify them with an lapply statement, but I don't know how to manipulate them.
Thanks!
Upvotes: 1
Views: 2739
Reputation: 104
Instead of
l1[1] <- data.frame(l1[1])[,-1]
use
l1[[1]] <- data.frame(l1[1])[,-1]
Upvotes: 3