我的问题与此类似,但过滤条件不同。
> demo(dadmom,package="tidyr")
> library(tidyr)
> library(dplyr)
> dadmom <- foreign::read.dta("http://www.ats.ucla.edu/stat/stata/modules/dadmomw.dta")
> dadmom %>%
+ gather(key, value, named:incm) %>%
+ separate(key, c("variable", "type"), -2) %>%
+ spread(variable, value, convert = TRUE)
famid type inc name
1 1 d 30000 Bill
2 1 m 15000 Bess
3 2 d 22000 Art
4 2 m 18000 Amy
5 3 d 25000 Paul
6 3 m 50000 Pat
使用原始表中的“incm”很容易找出妈妈收入>20000的家庭:
> dadmom
famid named incd namem incm
1 1 Bill 30000 Bess 15000
2 2 Art 22000 Amy 18000
3 3 Paul 25000 Pat 50000
问题是:你如何从“整理”的数据中做到这一点?