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

如何动态删除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"



时间:

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

要删除数据帧MyData中的变量VAR,请执行以下操作:


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



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


For (var in colnames(MyData)) {


 attr(MyData[,deparse(as.name(var))],"ATT_1") <- NULL


 attr(MyData[,deparse(as.name(var))],"ATT_2") <- NULL


}



希望这可以帮助你


for (var in colnames(MyData)) {


 attr(MyData[[deparse(as.name(var))]],"ATT_1") <- NULL


 attr(MyData[[deparse(as.name(var))]],"ATT_2") <- NULL


}



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


one_entry <- function(x) {


 for (i in length(x)) attr(x[[i]],"names") <- NULL


 return(x)


}



然后运行lapply


lapply(my_list, FUN=one_entry)



其中,mylist是问题中的数据结构。

这是个hack,但解决了我的问题。


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



稍微修改成


my_list <- lapply(my_list, FUN=one_entry)



数据帧中的远程所有attr属性条目:

df <- as.data.frame(lapply(df, as.vector))

...