如何对列表中所有数据框的列进行复杂的编辑? [英] How to do a complex edit of columns of all data frames in a list?

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

问题描述

我有一个称为WaFramesNumeric的185个数据帧的列表.每个数据框都有几百列和几千行.我想编辑每个数据框,以便它保留我指定的所有数字列以及所有非数字列.

I have a list of 185 data frames called WaFramesNumeric. Each dataframe has several hundred columns and thousands of rows. I want to edit every data frame, so that it leaves all numeric columns as well as any non-numeric columns that I specify.

使用:

for(i in seq_along(WaFramesNumeric)) {
    WaFramesNumeric[[i]] <- WaFramesNumeric[[i]][,sapply(WaFramesNumeric[[i]],is.numeric)] 
}

成功地使每个数据框仅包含其数字列.

successfully makes each dataframe contain only its numeric columns.

我尝试通过添加特定列的行来对此进行修改.我已经尝试过:

I've tried to amend this with lines to add specific columns. I have tried:

for (i in seq_along(WaFramesNumeric)) {
    a <- WaFramesNumeric[[i]]$Device_Name
    WaFramesNumeric[[i]] <- WaFramesNumeric[[i]][,sapply(WaFramesNumeric[[i]],is.numeric)] 
    cbind(WaFramesNumeric[[i]],a)
}

并尝试调用所有整数列的列号以及特定的列号,然后根据该列号进行组合:

and in an attempt to call the column numbers of all integer columns as well as the specific ones and then combine based on that:

for (i in seq_along(WaFramesNumeric)) {
    f <- which(sapply(WaFramesNumeric[[i]],is.numeric))
    m <- match("Cost_Center",colnames(WaFramesNumeric[[i]]))
    n <- match("Device_Name",colnames(WaFramesNumeric[[i]]))
    combine <- c(f,m,n)
    WaFramesNumeric[[i]][,i,combine]
}

所有这些都返回错误,我对如何做到这一点感到困惑. WaFramesNumeric是另一个数据帧列表(WaFramesNumeric <- WaFramesAll)的副本,因此我也尝试添加WaFramesAll中的特定列,但这并不成功.

These all return errors and I am stumped as to how I could do this. WaFramesNumeric is a copy of another list of dataframes (WaFramesNumeric <- WaFramesAll) and so I also tried adding the specific columns from the WaFramesAll but this was not successful.

对于您能提供的任何建议,我们深表谢意,如果其中任何一项不清楚,我们深表歉意.

I appreciate any advice you can give and I apologize if any of this is unclear.

推荐答案

您错误地认为for循环中的最后一个命令是有意义的.它不是.实际上,它已被丢弃,因此由于您从未将它分配到任何地方(cbindWaFramesNumeric...的索引),因此它被静默丢弃.

You are mistakenly assuming that the last commmand in a for loop is meaningful. It is not. In fact, it is being discarded, so since you never assigned it anywhere (the cbind and the indexing of WaFramesNumeric...), it is silently discarded.

此外,您在第三个代码块中对data.frame进行了过度索引.首先,它使用data.frame中的i,即使i是data.frames的list中的索引,而不是帧本身.其次(可能是由此引起的),您尝试索引2D框架的三个维度.只需将最后一个索引从[,i,combine]更改为[,combine][combine].

Additionally, you are over-indexing your data.frame in the third code block. First, it's using i within the data.frame, even though i is an index within the list of data.frames, not the frame itself. Second (perhaps caused by this), you are trying to index three dimensions of a 2D frame. Just change the last indexing from [,i,combine] to either [,combine] or [combine].

第三个问题(尽管也许尚未看到)是如果未找到任何内容,match将返回NA.用NA索引框架会返回错误(请尝试mtcars[,NA]查看).我建议您可以用grep替换match:什么都没找到时返回integer(0),在这种情况下就是您想要的.

Third problem (though perhaps not seen yet) is that match will return NA if nothing is found. Indexing a frame with an NA returns an error (try mtcars[,NA] to see). I suggest that you can replace match with grep: it returns integer(0) when nothing is found, which is what you want in this case.

for (i in seq_along(WaFramesNumeric)) {
  f <- which(sapply(WaFramesNumeric[[i]], is.numeric))
  m <- grep("Cost_Center", colnames(WaFramesNumeric[[i]]))
  n <- grep("Device_Name", colnames(WaFramesNumeric[[i]]))
  combine <- c(f,m,n)
  WaFramesNumeric[[i]] <- WaFramesNumeric[[i]][combine]
}

这篇关于如何对列表中所有数据框的列进行复杂的编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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