操作列表内的数据框 [英] Manipulate data frame inside a list
问题描述
这大概是一个简单的问题,但是我一直找不到答案。
我想删除列表内数据框的一列。
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]
考虑以下示例,其中两个数据帧df1和df2在列表l1中。
我想从df1中删除列x1。如果这只是一个数据帧,那么这很简单。
但是一旦进入列表,我不确定如何操作此数据框。
当我尝试在上一个语句中覆盖它时,出现错误。
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.
我的实际问题是列表中有大约100个数据帧,而大约10%其中有一个我需要删除的附加列。我可以轻松地识别它们,但我不知道如何操纵它们。
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.
谢谢!
推荐答案
代替
l1[1] <- data.frame(l1[1])[,-1]
使用
l1[[1]] <- data.frame(l1[1])[,-1]
这篇关于操作列表内的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!