[英]Reshape but expand the data in R
我有以下數據集:
my.data <- read.table(text = '
ID tmc_code wDay time_category TTTR
1 121-04711 weekday Afternoon 1.1
2 121-04711 weekend Evening 1.3
3 121-04711 weekday Morning 1.1
4 121-04712 weekend Afternoon 1.101626016
5 121-04712 weekday Evening 1.281124498
6 121-04712 weekday Morning 1.080645161
', header = TRUE, stringsAsFactors = FALSE, na.strings = 'NA')
my.data
我希望有一個像這樣的寬格式結果:
#result
# tmc_code wDay TTTR_afternnon TTTR_Evening TTTR_Morning
# 121-04711 weekday 1.1 1.3 NA
# 121-04711 weekend NA NA 1.1
# 121-04712 weekday NA 1.281124498 1.080645161
# 121-04712 weekend 1.101626016 NA NA
我們可以看到帽子不僅應該使用重塑功能,而且實際上這個過程將把6個數據變成9個數據。
下面的重塑功能不適用於這種情況:
w.my.data <- reshape(my.data, idvar = "tmc_code", timevar = "time_category", direction = "wide")
我想知道誰有更好的想法? 非常感激!
您可以使用reshape2包:
> reshape2::dcast(my.data, tmc_code + wDay ~ paste("TTTR", time_category, sep="_"))
Using TTTR as value column: use value.var to override.
tmc_code wDay TTTR_Afternoon TTTR_Evening TTTR_Morning
1 121-04711 weekday 1.100000 NA 1.100000
2 121-04711 weekend NA 1.300000 NA
3 121-04712 weekday NA 1.281124 1.080645
4 121-04712 weekend 1.101626 NA NA
哦,顯然它也適用於reshape
,這也提供了一個有用的警告,關於ID的變化在這里被忽略:
> reshape(my.data, idvar = c("tmc_code", "wDay"), timevar = "time_category", v.names = "TTTR", direction = "wide")
ID tmc_code wDay TTTR.Afternoon TTTR.Evening TTTR.Morning
1: 1 121-04711 weekday 1.100000 NA 1.100000
2: 2 121-04711 weekend NA 1.300000 NA
3: 4 121-04712 weekend 1.101626 NA NA
4: 5 121-04712 weekday NA 1.281124 1.080645
Warning message:
In reshapeWide(data, idvar = idvar, timevar = timevar, varying = varying, :
some constant variables (ID) are really varying
類似於tidyr::spread
的答案,但使用tidyr::spread
:
library(tidyverse)
my.data %>%
select(-ID) %>% # Be sure no important info is lost/misrepresented in dropping ID
mutate(time_category = paste0("TTTR", "_", time_category)) %>%
spread(time_category, TTTR)
tmc_code wDay TTTR_Afternoon TTTR_Evening TTTR_Morning
1 121-04711 weekday 1.100000 NA 1.100000
2 121-04711 weekend NA 1.300000 NA
3 121-04712 weekday NA 1.281124 1.080645
4 121-04712 weekend 1.101626 NA NA
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.