简体   繁体   中英

R: Gather/Spread/Reshape 21 Columns Based on 21 Other Column s

I would like to create columns based on values in some fields, populated by values in other fields. For example column1_time has value "1030" and column1_status has value "booked". I would like to pivot those into a new field time1030 with value "booked." There are 21 unique columns with times, (the times are only listed once per row, so they are unique across the 21 columns) -- and there are 21 unique columns with statuses that map back to the time columns. So these 42 time+status columns should be rearranged to one column per unique time, being populated by that time's corresponding status.

I have data that looks like this:

在此处输入图片说明

I would like to utilize R's gather/spread or reshape2 (legacy) functionality to transpose this data to look like this: 在此处输入图片说明

I tinkered around with gather and spread for a few hours but couldn't figure it out. I thought setting the key to ends_with('_time') and the value to ends_with('_status') might work but it did not from my attempts.

For a reproducible example of the data:

structure(list(appointment1_time = c("1030", "1030"), appointment2_time = c("1100", 
"1100"), appointment3_time = c("1130", "1130"), appointment4_time = c("1200", 
"1200"), appointment5_time = c("1230", "1230"), appointment6_time = c("0100", 
"0100"), appointment7_time = c("0130", "0130"), appointment8_time = c("0200", 
"0200"), appointment9_time = c("0230", "0230"), appointment10_time = c("0300", 
"0300"), appointment11_time = c("0330", "0330"), appointment12_time = c("0400", 
"0400"), appointment13_time = c("0430", "0430"), appointment14_time = c("0500", 
"0500"), appointment15_time = c("0530", "0530"), appointment16_time = c("0600", 
""), appointment17_time = c("0630", ""), appointment18_time = c("0700", 
""), appointment19_time = c("0730", ""), appointment20_time = c(NA_character_, 
NA_character_), appointment21_time = c(NA_character_, NA_character_
), appointment1_status = c("booked", "available"), appointment2_status = c("booked", 
"available"), appointment3_status = c("booked", "available"), 
    appointment4_status = c("booked", "available"), appointment5_status = c("booked", 
    "available"), appointment6_status = c("booked", "available"
    ), appointment7_status = c("booked", "available"), appointment8_status = c("booked", 
    "available"), appointment9_status = c("booked", "available"
    ), appointment10_status = c("booked", "available"), appointment11_status = c("booked", 
    "available"), appointment12_status = c("available", "available"
    ), appointment13_status = c("available", "available"), appointment14_status = c("available", 
    "available"), appointment15_status = c("booked", "available"
    ), appointment16_status = c("available", ""), appointment17_status = c("available", 
    ""), appointment18_status = c("available", ""), appointment19_status = c("available", 
    ""), appointment20_status = c(NA_character_, NA_character_
    ), appointment21_status = c(NA_character_, NA_character_)), row.names = 1:2, class = "data.frame")

A solution using tidyverse .

library(tidyverse)

# Get the time order
ord <- dat %>% select(ends_with("time")) %>% slice(1) %>% unlist()
# Remove NA
ord <- ord[!is.na(ord)]

dat2 <- dat %>%
  rowid_to_column() %>%
  gather(Column, Value, -rowid) %>%
  separate(Column, into = c("Apt", "time/status"), sep = "_") %>%
  spread(`time/status`, Value) %>%
  # Remove NA or "" in the status column
  filter(!is.na(status) & !status %in% "") %>%
  mutate(Apt = str_c("apt_slot", time, sep = "_")) %>%
  select(-time) %>%
  spread(Apt, status) %>%
  select(-rowid) %>%
  # Reorder the column
  select(str_c("apt_slot", ord, sep = "_"))

dat2
# apt_slot_1030 apt_slot_1100 apt_slot_1130 apt_slot_1200 apt_slot_1230 apt_slot_0100 apt_slot_0130
# 1        booked        booked        booked        booked        booked        booked        booked
# 2     available     available     available     available     available     available     available
# apt_slot_0200 apt_slot_0230 apt_slot_0300 apt_slot_0330 apt_slot_0400 apt_slot_0430 apt_slot_0500
# 1        booked        booked        booked        booked     available     available     available
# 2     available     available     available     available     available     available     available
# apt_slot_0530 apt_slot_0600 apt_slot_0630 apt_slot_0700 apt_slot_0730
# 1        booked     available     available     available     available
# 2     available          <NA>          <NA>          <NA>          <NA>

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