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

前端 未结 6 544
太阳男子
太阳男子 2020-12-17 14:47

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

List of 1
 $ 0021400001:List of 19
   $ GameSumma         


        
相关标签:
6条回答
  • 2020-12-17 14:59

    It's a hack but worked in my case.

    lapply(my_list, FUN=function(x){data.frame(as.matrix(x),stringsAsFactors = F)})
    
    0 讨论(0)
  • 2020-12-17 15:00

    Minor modification to Parisa's code worked for me:

    for (var in colnames(MyData)) {
      attr(MyData[[deparse(as.name(var))]], "ATT_1") <- NULL
      attr(MyData[[deparse(as.name(var))]], "ATT_2") <- NULL
    }
    
    0 讨论(0)
  • 2020-12-17 15:04

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

    my_list <- lapply(my_list, FUN=one_entry)
    
    0 讨论(0)
  • This is perhaps too late to answer on this thread, but I wanted to share.

    Two solutions : 1. function stripAttributes from merTools package.

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

      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

    0 讨论(0)
  • 2020-12-17 15:18

    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(my_list, FUN=one_entry)
    

    where mylist is the data structure in the question.

    0 讨论(0)
  • 2020-12-17 15:22

    Building up on everything that has already been said, the code below would keep some attributes and remove others for all your variables in data df. Just replace the attributes you want to keep in vector to_keep. In this case, I am assuming I want to keep attributes "label" and "at1"--assuming they exist, of course.

    to_keep <- c("label", "at1")
    
    to_keep_regx <- paste(to_keep, collapse = "|")
      
    nn  <- names(df)
    for (x in seq_along(nn)) {
      
      ats       <- attributes(df[[x]])
      atsn      <- names(ats)
      to_remove <- atsn[!grepl(to_keep_regx, atsn)]
      
      for (i in seq_along(to_remove)) {
        attr(df[[x]], to_remove[i]) <- NULL
      }
    
    }
    
    

    Best,

    0 讨论(0)
提交回复
热议问题