list 删除R中多级列表上data.frames中列的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28873323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:07:06  来源:igfitidea点击:

Removing attributes of columns in data.frames on multilevel lists in R

rlist

提问by geodex

How do I remove the attributes of the following columns of data.frames on a nested list in R on the fly?

如何动态删除 R 中嵌套列表中 data.frames 以下列的属性?

List of 1
 $ 0021400001:List of 19
   $ GameSummary      :'data.frame':    1 obs. of  13 variables:
    $ GAME_DATE_EST                   : Factor w/ 1 level "2014-11-09T00:00:00": 1
      - attr(*, "names")= chr "1"
    $ GAME_SEQUENCE                   : Factor w/ 1 level "2": 1
      - attr(*, "names")= chr "2"
    $ GAME_ID                         : Factor w/ 1 level "0021400091": 1
      - attr(*, "names")= chr "3"
    $ GAME_STATUS_ID                  : Factor w/ 1 level "3": 1
      - attr(*, "names")= chr "4" 
   $ SeasonSeries     :'data.frame':    1 obs. of  7 variables:
     $ GAME_ID         : Factor w/ 1 level "0021400001": 1
      - attr(*, "names")= chr "1"
     $ HOME_TEAM_ID    : Factor w/ 1 level "1610612740": 1
      - attr(*, "names")= chr "2"
     $ VISITOR_TEAM_ID : Factor w/ 1 level "1610612753": 1
      - attr(*, "names")= chr "3"

回答by Parisa

This is perhaps too late to answer on this thread, but I wanted to share.

在这个线程上回答可能为时已晚,但我想分享。

Two solutions : 1. function stripAttributes from merTools package.

两种解决方案: 1. merTools 包中的函数stripAttributes。

  1. to remove the attribute ATT from variable VAR in your data-frame MyData:

    attr(MyData$VAR, "ATT") <- NULL
    
  1. 从数据框 MyData 中的变量 VAR 中删除属性 ATT:

    attr(MyData$VAR, "ATT") <- NULL
    

If you want to remove several attributes of all variables :

如果要删除所有变量的几个属性:

For (var in colnames(MyData)) {
     attr(MyData[,deparse(as.name(var))], "ATT_1") <- NULL
     attr(MyData[,deparse(as.name(var))], "ATT_2") <- NULL
}

I hope This Helps, Regards

我希望这有帮助,问候

回答by Karsten W.

You could write a function that works on one entry in the list, e.g.

您可以编写一个对列表中的一个条目起作用的函数,例如

one_entry <- function(x) {
    for (i in length(x)) attr(x[[i]], "names") <- NULL
    return(x)
}

and then run lapply:

然后运行lapply

lapply(my_list, FUN=one_entry)

where mylistis the data structure in the question.

问题中mylist的数据结构在哪里。

回答by Aline

It's a hack but worked in my case.

这是一个黑客,但在我的情况下有效。

lapply(my_list, FUN=function(x){data.frame(as.matrix(x),stringsAsFactors = F)})

回答by adts

Minor modification to Parisa's code worked for me:

对Parisa代码的小修改对我有用:

for (var in colnames(MyData)) {
  attr(MyData[[deparse(as.name(var))]], "ATT_1") <- NULL
  attr(MyData[[deparse(as.name(var))]], "ATT_2") <- NULL
}

回答by JSB

Karsten W.'s answer worked for me as I had to remove more than 2 attributes. Slightly modified to

Karsten W. 的回答对我有用,因为我必须删除 2 个以上的属性。稍微修改为

my_list <- lapply(my_list, FUN=one_entry)