18

I want to erase all attributes from data and applied this solution. However neither one_entry() (the original) nor my one_entry2() will work and I don't see why.

one_entry2 <- function(x) {
  attr(x, "label") <- NULL
  attr(x, "labels") <- NULL
}

> lapply(df1, one_entry2)
$`id`
NULL

$V1
NULL

$V2
NULL

$V3
NULL

How can we do this?

Data:

df1 <- setNames(data.frame(matrix(1:12, 3, 4)), 
                c("id", paste0("V", 1:3)))
attr(df1$V1, "labels") <- LETTERS[1:4]
attr(df1$V1, "label") <- letters[1:4]
attr(df1$V2, "labels") <- LETTERS[1:4]
attr(df1$V2, "label") <- letters[1:4]
attr(df1$V3, "labels") <- LETTERS[1:4]
attr(df1$V3, "label") <- letters[1:4]

> str(df1)
'data.frame':   3 obs. of  4 variables:
 $ id: int  1 2 3
 $ V1: int  4 5 6
  ..- attr(*, "labels")= chr  "A" "B" "C" "D"
  ..- attr(*, "label")= chr  "a" "b" "c" "d"
 $ V2: int  7 8 9
  ..- attr(*, "labels")= chr  "A" "B" "C" "D"
  ..- attr(*, "label")= chr  "a" "b" "c" "d"
 $ V3: int  10 11 12
  ..- attr(*, "labels")= chr  "A" "B" "C" "D"
  ..- attr(*, "label")= chr  "a" "b" "c" "d"
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 2
    if anyone needs to remove all attributes from one numeric variable, this worked for me: `as.numeric(a)` – Tomas Jun 25 '20 at 16:19
  • `as.numeric` also removes the names. If you want to keep the names, in particular a named list, something like this could work `as.list(unlist(a))`. – passerby51 Jun 24 '22 at 07:25
  • Or to remove other attributes but keep names for a vector, you can simply use `c(a)`. – nisetama Jun 21 '23 at 12:32

6 Answers6

29

To remove all attributes, how about this

df1[] <- lapply(df1, function(x) { attributes(x) <- NULL; x })
str(df1)
#'data.frame':  3 obs. of  4 variables:
# $ id: int  1 2 3
# $ V1: int  4 5 6
# $ V2: int  7 8 9
# $ V3: int  10 11 12
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • Dear Maurits may I ask what actually `df1[]` does here? I have never encountered this structure before. Thanks in advance. – Anoushiravan R Nov 07 '21 at 01:24
  • 2
    Hi @AnoushiravanR. Rather than me doing a poor job rephrasing what other's have said better, have a look [here](https://stackoverflow.com/questions/21165588/what-does-df-do-in-r). – Maurits Evers Nov 07 '21 at 10:30
  • 1
    Thank you very much for your reply and the link. No you absolutely did a great job here :) Best wishes. – Anoushiravan R Nov 07 '21 at 11:17
8

Simplifying a bit @maurits-evers answer:

df1[] <- lapply(df1, as.vector)
str(df1)
#'data.frame':  3 obs. of  4 variables:
# $ id: int  1 2 3
# $ V1: int  4 5 6
# $ V2: int  7 8 9
# $ V3: int  10 11 12

The original answer is by Prof. Brian Ripley in this R-Help post.

In tidyverse world:

df1 <- df1 %>% mutate(across(everything(), as.vector))

With data.table

library(data.table)
# Assuming
# setDT(df1) # or
# df1 <- as.data.table(df1)

df1 <- df1[, lapply(.SD, as.vector)] 
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
iago
  • 2,990
  • 4
  • 21
  • 27
3

Provided all the columns are the same type (as in your example) you can do

df1[] = c(df1, recursive=TRUE)
dww
  • 30,425
  • 5
  • 68
  • 111
2

The PKPDmisc package has a dplyr friendly way to do this:

library(PKPDmisc)
df %>% strip_attributes(c("label", "labels"))
atmo
  • 66
  • 1
  • 6
2

The following is a simple solution (and will not convert a date class to a numeric):

df1 <- data.frame(df1)
Azriel
  • 21
  • 2
  • I do not know why but this was actually the only solution that was working for me. Thank you very much! – U_jex Dec 16 '22 at 13:05
1

For certain situations, a modified version of the answer by @maurits-evers may be useful.

Create a function to remove attributes.

remove_attributes <- function(x) {attributes(x) <- NULL; return(x)}

To remove attributes from one element in a list.

df1$V1 <- remove_attributes(df1$V1)

To remove attributes from all elements in a list.

df1 <- lapply(df1, remove_attributes)
Agriculturist
  • 521
  • 9
  • 20