简体   繁体   English

在R中-如何删除data.frame中的特定列?

[英]in R - How can I remove specific columns in data.frame?

In this data.frame: 在此data.frame中:

在此处输入图片说明

I want to remove some columns that "Total" == 0 我想删除"Total" == 0某些列

I reviewed some questions about my problems in here and I found it 我在这里回顾了一些有关我的问题的问题,然后找到了

R: how to remove certain rows in data.frame R:如何删除data.frame中的某些行

but I can't apply perfectly in my case. 但我不能完美地适用于我的情况。

Assuming that the "Total" in the "x' column denotes the last row (I have no reason to think otherwise), we subset the last row ( nrow(df1) ) and remove the first column (as it is non-numeric), check whether that is not equal to 0. We get a logical vector from that. Concatenate ( c ) with TRUE so that we can also get the first column ie "x" in the subset dataset. 假定“ x”列中的“总计”表示最后一行(我没有其他理由认为),我们将最后一行( nrow(df1) )作为子集并删除第一列(因为它是非数字) ,检查是否等于0,从中得到一个逻辑vector ,将( c )与TRUE以便我们还可以获得子集数据集中的第一列,即“ x”。

df1[c(TRUE,df1[nrow(df1),-1]!=0)]
#      x v2 v3
#1     a  1  0
#2     b  3  1
#3 Total  4  1

data 数据

df1 <- data.frame(x= c('a', 'b', 'Total'),
     v1= c(0,0,0), v2= c(1,3,4), v3=c(0, 1,1))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用R删除data.frame中特定列中的字符? - How to remove a character in specific columns in a data.frame with R? R-如何在不使用sqldf的情况下从data.frame的多个列中删除行? - R - How can I remove rows from multiple columns in a data.frame without using sqldf? 如何使用apply分解R中data.frame中的特定列 - How to factorize specific columns in a data.frame in R using apply 如何从多个data.frame中获取特定列并将其保存为R中的新data.frame? - how to grab specific columns from multiple data.frame and save it as a new data.frame in R? 如何在现有data.frame中添加其他列,这些列在data.frame中已有的一个特定列上对齐? - How can I add additional columns to an existing data.frame, that are aligned on one specific column already in the data.frame? 如何从 data.frame 中删除两个特定列有缺失值的行? - How do I remove rows from a data.frame where two specific columns have missing values? 在R中,如何合并data.frame中的两列 - In R, how can I combine two columns within a data.frame 如何使用R中的自定义函数聚合data.frame中的多个列? - How can I aggregate multiple columns in a data.frame with a custom function in R? 如何在 data.frame R 中对连续列进行第 n 次求和 - How can I sum consecutive columns nth times in a data.frame R 如何从R中的data.frame文件中删除文本中的标点符号和数字 - How can I remove punctuations and numbers in text from data.frame file in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM