我有这个数据集,我希望将其重塑为长格式:(更新链接)https://drive.google.com/file/d/1EqD58WVXm-v35Jr6GAwL6arxzO_tTyFa/view?usp=sharing
在重塑的手动方法中,我这样做:
census_data <- read.csv(file.choose())
colnames(census_data)[8] <- "CENSUSPOP2010"
census_long <- census_data %>%
gather(Var, Value, -SUMLEV, -REGION, -DIVISION, -STATE, -COUNTY, -STNAME, -CTYNAME) %>%
mutate(Time = sub("[A-Za-z]+", "", Var), Type = sub("[\\_0-9]+", "", Var)) %>%
select(-Var) %>%
spread(Type, Value)
但是我希望独立的变量被引用,所以很容易将它传递给自定义函数,所以我不得不尝试像这样定义它:
var <- c("SUMLEV", "REGION", "DIVISION", "STATE", "COUNTY", "STNAME", "CTYNAME")
var_enquos <- enquos(var)
census_long <- census_data %>%
gather(Var, Value, -!!!var_enquos) %>%
mutate(Time = sub("[A-Za-z]+", "", Var), Type = sub("[\\_0-9]+", "", Var)) %>%
select(-Var) %>%
spread(Type, Value)
但这会产生:Error: Can't use !!! at top level.
难道这种做法都不可能吗?欢迎任何建议,谢谢