删除列表中数据框中的一列 [英] Delete a column in a data frame within a list

查看:398
本文介绍了删除列表中数据框中的一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据A列中的因子水平从数据框中列出了一个列表.在列表中,我想删除该列.我的脑袋说得很烂,但没有其他:P

I made a list out of my dataframe, based on the factor levels in column A. In the list I would like to remove that column. My head is saying lapply, but not anything else :P

$A
ID Test
A   1
A   1

$B
 ID Test
 B   1
 B   3
 B   5

进入

$A
Test
 1
 1

$B
Test
 1
 3
 5

推荐答案

假设您的列表名为myList,则类似这样的方法应该起作用:

Assuming your list is called myList, something like this should work:

lapply(myList, function(x) { x["ID"] <- NULL; x })

更新

对于更通用的解决方案,您还可以使用以下内容:

Update

For a more general solution, you can also use something like this:

# Sample data
myList <- list(A = data.frame(ID = c("A", "A"), 
                              Test = c(1, 1), 
                              Value = 1:2), 
               B = data.frame(ID = c("B", "B", "B"), 
                              Test = c(1, 3, 5), 
                              Value = 1:3))
# Keep just the "ID" and "Value" columns
lapply(myList, function(x) x[(names(x) %in% c("ID", "Value"))])
# Drop the "ID" and "Value" columns
lapply(myList, function(x) x[!(names(x) %in% c("ID", "Value"))])

这篇关于删除列表中数据框中的一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆