我的数据框如下所示:
Type Availability Count
DayTime Cancelled 10
Morning Cancelled 15
Night Cancelled 13
DayTime Trip Completed 14
Morning Trip Completed 71
Night Trip Completed 32
DayTime Not-Present 13
Morning Not-Present 43
Night Not-Present 23
我想将此数据帧转换为只有3行的格式,即DayTime,Morning和Night。 3列,即已取消,已完成旅行和未存在,具有相应的计数。 我怎么能在R?请帮忙。
答案 0 :(得分:0)
查看tidyr::spread
。
my_data <- tibble::tribble(
~Type, ~Availability, ~Count,
"DayTime", "Cancelled", 10,
"Morning", "Cancelled", 15,
"Night", "Cancelled", 13,
"DayTime", "Trip Completed", 14,
"Morning", "Trip Completed", 71,
"Night", "Trip Completed", 32,
"DayTime", "Not-Present", 13,
"Morning", "Not-Present", 43,
"Night", "Not-Present", 23
)
my_data %>% tidyr::spread(key = Availability, value = Count)