1
> x <- data.frame(
+   blah = 1:10
+ ) %>% 
+   mutate(`two words` = rnorm(10, 0, 1))
> x
   blah   two words
1     1  0.73446160
2     2  0.75684241
3     3  0.02717962
4     4 -1.12091171
5     5 -1.26650625
6     6  0.20336003
7     7 -0.56643766
8     8 -0.17442082
9     9 -0.85714736
10   10  0.49964362

I'd like to wrap two words heading onto two lines.

Tried:

> x <- data.frame(
+   blah = 1:10
+ ) %>% 
+   mutate(`two words` = rnorm(10, 0, 1)) %>% 
+   rename(str_wrap(`two words`, width = 15) = `two words`)
Error: unexpected '=' in:
"  mutate(`two words` = rnorm(10, 0, 1)) %>% 
  rename(str_wrap(`two words`, width = 15) ="

How can I rename a header with a space in order to wrap the text?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • 2
    I think you want to use this method with `!!` - https://stackoverflow.com/a/46973165/496803 - something like: `rename(!!str_wrap("two words", width = 15) := \`two words\`)` – thelatemail Sep 26 '19 at 00:13
  • 2
    Alternately, you can use `rename_at(vars(\`two words\`), str_wrap, 15)`. – Ritchie Sacramento Sep 26 '19 at 00:25
  • I am not really sure if I understand what you mean by `two words heading onto two lines`. can you show your expected output? – Ronak Shah Sep 26 '19 at 03:00
  • The comments above worked for me if you either of you want to make them an answer. @RonakShah I just mean test wrap so when the table is printed out the table headings are text wrapped – Doug Fir Sep 26 '19 at 03:25

0 Answers0