How to erase all attributes?

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

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

df1[] = c(df1, recursive=TRUE)

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)] 

Tags:

R

Attr