[英]Problems using tidyr's pivot_wider on multiple columns
I am trying to use the pivot_wider function from tidyr to transpose two kinds of value at the same time, as shown in the "Multiple observations per row" example under vignette('pivot'), but I keep getting strange error messages.我正在尝试使用 tidyr 中的 pivot_wider 函数同时转置两种值,如小插图('pivot')下的“每行多个观察”示例所示,但我不断收到奇怪的错误消息。
Here is an example of what is happening:这是正在发生的事情的一个例子:
set.seed(5)
testdat <- data.frame(matrix(nrow=5,ncol=5))
colnames(testdat) <- c('rating','percent.Female','percent.Male','se.Female','se.Male')
testdat$rating <- c('Very good','Good','OK','Bad','Very bad')
testdat$percent.Female <- rnorm(5,.5,.2)
testdat$percent.Male <- 1 - testdat$percent.Female
testdat$se.Female <- rnorm(5,0.1,0.003)
testdat$se.Male <- rnorm(5,0.1,0.003)
testdat
rating percent.Female percent.Male se.Female se.Male
1 Very good 0.3318289 0.6681711 0.09819128 0.10368289
2 Good 0.7768719 0.2231281 0.09858350 0.09759466
3 OK 0.2489016 0.7510984 0.09809389 0.09675882
4 Bad 0.5140286 0.4859714 0.09914268 0.09952740
5 Very bad 0.8422882 0.1577118 0.10041432 0.09678472
testdat %>% pivot_longer(cols=-"rating",names_sep=".",names_to=c(".value","gender"),values_drop_na=T)
Error: Expected a vector, not NULL Call `rlang::last_error()` to see a backtrace In addition: Warning message: Expected 2 pieces. Additional pieces discarded in 4 rows [1, 2, 3, 4]
I followed the vignette almost exactly - why isn't this working?我几乎完全按照小插图 - 为什么这不起作用?
The problems with the code are happening because of the option names_sep="."
由于选项names_sep="."
(you'll notice in the pivot vignette, names are separated by _ instead of .) (您会注意到在枢轴小插图中,名称由 _ 而不是 . 分隔。)
.
is a special character intended to match any single character.是用于匹配任何单个字符的特殊字符。 If you want to specify that your variable names are separated by the actual .
如果要指定变量名称由实际的.
character itself, you need to use names_sep="\\\\."
字符本身,您需要使用names_sep="\\\\."
to escape it.逃避它。
With the escaping in place, the example comes out like this:转义到位后,示例如下:
testdat %>%
pivot_longer(cols=-"rating", names_sep="\\.",
names_to=c(".value","gender"), values_drop_na=TRUE)
# A tibble: 10 x 4
rating gender percent se
<chr> <chr> <dbl> <dbl>
1 Very good Female 0.332 0.0982
2 Very good Male 0.668 0.104
3 Good Female 0.777 0.0986
4 Good Male 0.223 0.0976
5 OK Female 0.249 0.0981
6 OK Male 0.751 0.0968
7 Bad Female 0.514 0.0991
8 Bad Male 0.486 0.0995
9 Very bad Female 0.842 0.100
10 Very bad Male 0.158 0.0968
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.