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
It's a hack but worked in my case.
lapply(my_list, FUN=function(x){data.frame(as.matrix(x),stringsAsFactors = F)})
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
}
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)
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.
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,