删除R中不同数据集中的多个列 [英] Deleting multiple columns in different data sets in R

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

问题描述

我想知道是否有一个很好的方法来删除R中的几个不同的数据集的多个列。我有一个数据集,看起来像:

  RangeNumber时间价值质量批准
1 2:00 1 1 1
2 2:05 4 2 1

我想删除数据集中的时间和值列。我通过设置每列为NULL来删除它们,例如: data1 $ RangeNumber< - NULL



<我将有16个或更多的数据集,具有相同的列设置,数据集将按增量顺序编号,例如:data1,data2,data3,& c。



我想知道,如果循环遍历所有数据集列的 循环是实现这个目标的最好方法,因为我已经读过R在对于循环很慢 - 如果有一个更简单的方法来做到这一点。我还想知道是否需要将所有数据集合到一个变量中,然后迭代删除列。



如果 for 循环是最好的方法,如何设置它?想要将这些数据帧收集到一个列表中,然后通过它们运行提取功能。给的第一个参数应该是TRUE,这样就可以获得所有的行,而第二个参数应该是列名(我构成了三个数据框,它们的行号和列名是不同的,但都有时间和值'列:

 > datlist<  -  list(dat1,dat2,dat3)
> TimVal< ; - lapply(datlist,[,TRUE,c(Time,Value))
> TimVal
[[1]]
时间值
1 2:00 1
2 2:05 4

[[2]]
时间价值
1 2:00 1
2 2:05 4

[[3]]
时间价值
1 2:00 1
2 2:05 4
2.1 2:05 4
1.1 2:00 1

如果目标是将它们放在一起dataframe:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $ 2 $ 1
2 2:05 4
3 2:00 1
4 2:05 4
11 2:00 1
21 2:05 4
2.1 2:05 4
1.1 2:00 1

如果你对R很新,你可能还没有想出最后一个代码没有改变TimVal;它只显示将返回什么值,并使该效果持久,您将需要分配给一个名称。也许甚至是同一个名字:

  TimVal < -  do.call(rbind,TimVal):


I'm wondering if there is a good way to delete multiple columns over a few different data sets in R. I have a data set that looks like:

RangeNumber    Time    Value    Quality    Approval
          1    2:00        1          1           1
          2    2:05        4          2           1

And I want to delete everything but the Time and Value columns in my data sets. I'm "deleting" them by setting each column to NULL, e.x.: data1$RangeNumber <- NULL.

I'm going to have upwards of 16 or more data sets with identical column setups, and data sets are going to be numbered in incremental order, e.x.: data1, data2, data3, &c.

I'm wondering if a for loop that iterates through all of the data set columns is the best way to accomplish this, or -- since I have read that R is slow at for loops-- if there is an easier way to do this. I'm also wondering if I need to combine all of my data sets into one variable, and then iterate through to remove the columns.

If a for loop is the best way to go, how would I set it up?

解决方案

You want to gather those dataframes into a list and then run the Extract function over them. The first argument given to "[" should be TRUE so that all rows are obtained, and the second argument should be the column names (I made up three dataframes that varied in their row numbers and column names but all had 'Time' and 'Value' columns:

> datlist <- list(dat1,dat2,dat3)
> TimVal <- lapply(datlist, "[", TRUE, c("Time","Value") )
> TimVal
[[1]]
  Time Value
1 2:00     1
2 2:05     4

[[2]]
  Time Value
1 2:00     1
2 2:05     4

[[3]]
    Time Value
1   2:00     1
2   2:05     4
2.1 2:05     4
1.1 2:00     1

This is added in case the goal was to have them all together in the same dataframe:

> do.call(rbind, TimVal)
    Time Value
1   2:00     1
2   2:05     4
3   2:00     1
4   2:05     4
11  2:00     1
21  2:05     4
2.1 2:05     4
1.1 2:00     1

If you are very new to R you may not have figured out that the last code did not change TimVal; it only showed what value would be returned and to make the effect durable you would need to assign to a name. Perhaps even the same name:

TimVal <- do.call(rbind, TimVal):

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

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