简体   繁体   中英

collapse 2-col data frame where col1 contains names, col2 contains values

I think there must be an easy way to collapse the input table to yield the desired output table, but I'm blanking on it.

library(tidyverse)
input <- tribble(
  ~name, ~value,
  "animal", "pig",
  "animal", "dog",
  "animal", "cat",
  "plant", "tree",
  "plant", "bush",
  "plant", "flower"
)

output <- tribble(
  ~animal, ~plant,
  "pig", "tree",
  "dog", "bush",
  "cat", "flower"
)

In input , col1 contains the variable label for each value in col2. In output , the table is reformatted so that the values in input$value appear in columns named according to the corresponding elements in input$name .

We can use unstack from base R (no packages used)

unstack(input, value ~ name)
#   animal  plant
#1    pig   tree
#2    dog   bush
#3    cat flower

Or with dcast from data.table

library(data.table)
dcast(input, rowid(name)~ name)[,-1]
#    animal  plant
#1    pig   tree
#2    dog   bush
#3    cat flower

Or using dplyr

library(dplyr)
input %>% 
    group_split(name, keep = FALSE) %>% 
    bind_cols

Or using split

split(input$value, input$name) %>% 
         bind_cols

Or another option with spread

library(tidyr)
input %>%
   mutate(rn = rowid(name)) %>% 
   spread(name, value)

We can create a row_number() for every name and then spread

library(dplyr)
library(tidyr)

input %>%
  group_by(name) %>%
  mutate(row = row_number()) %>%
  spread(name, value) %>%
  select(-row)

#   animal plant 
#  <chr>  <chr> 
#1  pig    tree  
#2  dog    bush  
#3  cat    flower

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM