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"
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.
To remove first few rows from each group in R, we can use slice function of dplyr package after grouping with group_by function.
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.
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.
This is perhaps too late to answer on this thread, but I wanted to share.
Two solutions : 1. function stripAttributes from merTools package.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With