在R中转换数据帧格式

时间:2017-10-08 11:15:50

标签: r dataframe data-cleaning

我的数据框如下所示:

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?请帮忙。

1 个答案:

答案 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)