我有大量数据框,包含不同的组:
df <- data.frame(
Week=c("Week1", "Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1"),
Day = c("Monday", "Monday", "Monday", "Tuesday", "Tuesday", "Tuesday", "Wednesday", "Wednesday",
"Wednesday", "Thursday", "Thursday", "Thursday"),
Type=c("A", "B", "C", "A", "B", "C", "A", "B", "C","A", "B", "C" ),
Result=c("YES", "YES", "NO", "NO", "NO", "NO", "YES", "NO", "NO","YES", "YES", "NO")
我想添加另一个列,表示&#34; YES&#34;对于有结果的工作日&#34;是&#34;对于这两种类型&#34; A&#34;和&#34; B&#34;,就像这样:
df2 <- data.frame(
Week=c("Week1", "Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1"),
Day = c("Monday", "Monday", "Monday", "Tuesday", "Tuesday", "Tuesday", "Wednesday", "Wednesday",
"Wednesday", "Thursday", "Thursday", "Thursday"),
Type=c("A", "B", "C", "A", "B", "C", "A", "B", "C","A", "B", "C" ),
Result=c("YES", "YES", "NO", "NO", "NO", "NO", "YES", "NO", "NO","YES", "YES", "NO"),
Result.AB=c("YES", "YES", "YES", "NO", "NO", "NO", "NO", "NO", "NO","YES", "YES", "YES")
)
df2 有人能帮助我吗?谢谢=)
答案 0 :(得分:1)
这是使用tidyverse的解决方案。
library(tidyverse)
df <- data.frame(
Week=c("Week1", "Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1","Week1"),
Day = c("Monday", "Monday", "Monday", "Tuesday", "Tuesday", "Tuesday", "Wednesday", "Wednesday",
"Wednesday", "Thursday", "Thursday", "Thursday"),
Type=c("A", "B", "C", "A", "B", "C", "A", "B", "C","A", "B", "C" ),
Result=c("YES", "YES", "NO", "NO", "NO", "NO", "YES", "NO", "NO","YES", "YES", "NO")
)
df2 <- df %>%
mutate(Day = factor(Day, levels = c('Monday', 'Tuesday', 'Wednesday', 'Thursday'))) %>%
spread(key = Type, value = Result) %>%
mutate(Result.AB = ifelse(A == 'YES' & B == 'YES', 'YES', 'NO')) %>%
gather(key = Type, value = Result, -c(Week, Day, Result.AB)) %>%
arrange(Day, Type) %>%
select(Week, Day, Type, Result, Result.AB)
df2
#> Week Day Type Result Result.AB
#> 1 Week1 Monday A YES YES
#> 2 Week1 Monday B YES YES
#> 3 Week1 Monday C NO YES
#> 4 Week1 Tuesday A NO NO
#> 5 Week1 Tuesday B NO NO
#> 6 Week1 Tuesday C NO NO
#> 7 Week1 Wednesday A YES NO
#> 8 Week1 Wednesday B NO NO
#> 9 Week1 Wednesday C NO NO
#> 10 Week1 Thursday A YES YES
#> 11 Week1 Thursday B YES YES
#> 12 Week1 Thursday C NO YES
我已经使用spread
每天获取一行,mutate
计算新的Result.AB
变量,然后gather
再次分割观察结果。管道的其余部分将行按顺序排列。