Home > Mobile >  Remove labels from within data.frame and tibble
Remove labels from within data.frame and tibble

Time:11-18

I have used named vectors to make a data.frame. When I convert this data.frame to a tibble, I end up with named lists instead of regular columns. How can I remove these names?

example data

ww = structure(list(is_character = list(`labA` = 1, `labB` = 1, `labC` = 1), 
                    is_numeric = list(`labA` = 0, `labB` = 0, `labC` = 0),
                    is_logical = list(`labA` = 0, `labB` = 0, `labC` = 0),
                    column_name = c("labA", "labB", "labC")),
               row.names = c(NA, -3L), class = "data.frame")

Everything looks fine when viewed as a dataframe. as.data.frame(ww) returns the following:

  is_character is_numeric is_logical column_name
1            1          0          0        labA
2            1          0          0        labB
3            1          0          0        labC

But when converted to a tibble I end up with named lists instead of plain columns. as_tibble(ww) returns the following:

# A tibble: 3 x 4
  is_character is_numeric   is_logical   column_name
  <named list> <named list> <named list> <chr>      
1 <dbl [1]>    <dbl [1]>    <dbl [1]>    labA       
2 <dbl [1]>    <dbl [1]>    <dbl [1]>    labB       
3 <dbl [1]>    <dbl [1]>    <dbl [1]>    labC 

I don't want to work with named lists in my tibble, I want to work with plain numeric columns. How can I do this?

I have tried to make the original data.frame from unnamed lists, but this introduces other problems. So, removing the names is preferable. I have made several attempts with unname with no success.

CodePudding user response:

There are a couple of simple approaches:

as_tibble(ww) %>% mutate(across(everything(), unlist))

Or

as_tibble(ww) %>% unnest(everything())

Both approaches produce:

  is_character is_numeric is_logical column_name
         <dbl>      <dbl>      <dbl> <chr>      
1            1          0          0 labA       
2            1          0          0 labB       
3            1          0          0 labC  

Of note, your is_character column is not character, and your is_logical column is not logical. However, if your input data was this:

ww = structure(list(is_character = list(`labA` = "a", `labB` = "b", `labC` = "c"), 
                    is_numeric = list(`labA` = 0, `labB` = 0, `labC` = 0),
                    is_logical = list(`labA` = T, `labB` = F, `labC` = T),
                    column_name = c("labA", "labB", "labC")),
               row.names = c(NA, -3L), class = "data.frame")

then the above approaches would produce this:

# A tibble: 3 x 4
  is_character is_numeric is_logical column_name
  <chr>             <dbl> <lgl>      <chr>      
1 a                     0 TRUE       labA       
2 b                     0 FALSE      labB       
3 c                     0 TRUE       labC       
  • Related