简体   繁体   中英

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.

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="." (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="\\\\." 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM