df1 <- data.frame(
" " = c(" ", "Part Number 1", "Part Number 2", "Part Number 3"),
Julie = c("Measurement 1", 33, 34, 33),
Julie = c("Measurement 2", 32, 31, 31),
Joe = c("Measurement 1", 33, 33, 30),
Joe = c("Measurement 2", 31, 32, 31))
df1 %>%
mutate_all(as.character) %>%
set_names(c("Part", paste(names(.)[2:ncol(.)], .[1, 2:ncol(.)], sep = "-"))) %>%
`[`(2:nrow(.), ) %>%
gather("key", "value", contains("Measurement")) %>%
separate("key", c("person", "measurement"), sep = "-") %>%
mutate_at("person", ~ stringr::str_replace(.x, "\..*","")) # line 14
# mutate_at("person", ~ stringr::str_replace(., "\..*","")) # line 15
There's only one thing I want to highlight in the code above, and that's the difference between line #14 and line #15. Notice that the str_replace()
piped data set for line #14 is .x
and the piped data set for line #15 is just .
. I'm used to seeing things in the form of line #15, and it seems the x
in line #14 is benign.
But the x
is not benign. If I include it in similar mtcars code (below) I get an error instead of a 'TRUE'. Can you explain this? I've never seen this mysterious x
syntax before, which hardly means it's not important (or common).
identical(mtcars %>% .[1:2, ],
mtcars %>% .x[1:2, ])
# Error in `[.data.frame`(., .x, 1:2, ) : object '.x' not found
See Question&Answers more detail:
os