Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

list

r

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
   $ 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"
like image 549
geodex Avatar asked Mar 05 '15 08:03

geodex


People also ask

How do I remove column labels in R?

To remove the row names or column names from a matrix, we just need to set them to NULL, in this way all the names will be nullified.

How do I remove the first 5 rows in R?

To remove first few rows from each group in R, we can use slice function of dplyr package after grouping with group_by function.

What are attributes R?

Objects in R can have many properties associated with them, called attributes. These properties explain what an object represents and how it should be interpreted by R. Quite often, the only difference between two similar objects is that they have different attributes.

How do I remove all data frames in R?

The R programming language offers two helpful functions for viewing and removing objects within an R workspace: ls(): List all objects in current workspace. rm(): Remove one or more objects from current workspace.


2 Answers

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

like image 174
Parisa Avatar answered Oct 20 '22 01:10

Parisa


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.

like image 45
Karsten W. Avatar answered Oct 20 '22 01:10

Karsten W.