“”的含义是什么? &。”在tidyr :: separate? [英] What is the meaning of " \\." in tidyr::separate?
问题描述
\。的目的是什么,为什么要引用它?
这是代码:
what is the purpose of " \. " and why it is quoted? this is the code:
library(tidyr)
iris.tidy <- iris %>%
gather(key, Value, -Species) %>%
separate(key, c("Part", "Measure"), "\\.")
用于虹膜数据集
推荐答案
如果逐步运行代码,将会更容易理解。
It would be easier to understand if you run the code step by step.
聚集
使用列 key
具有列名和列 value
并具有那些列的值
gather
brings the data in long format with column key
with column names and column value
with values of those columns
library(tidyr)
iris %>% gather(key, Value, -Species) %>% head
# Species key Value
#1 setosa Sepal.Length 5.1
#2 setosa Sepal.Length 4.9
#3 setosa Sepal.Length 4.7
#4 setosa Sepal.Length 4.6
#5 setosa Sepal.Length 5.0
#6 setosa Sepal.Length 5.4
然后我们使用 separate
将键
列基于其文本中的。。
分为两列。
We then use separate
to divide key
column in two columns based on "."
in their text.
iris %>%
gather(key, Value, -Species) %>%
separate(key, c("Part", "Measure"), "\\.") %>% head
# Species Part Measure Value
#1 setosa Sepal Length 5.1
#2 setosa Sepal Length 4.9
#3 setosa Sepal Length 4.7
#4 setosa Sepal Length 4.6
#5 setosa Sepal Length 5.0
#6 setosa Sepal Length 5.4
由于 separate
中的 sep
参数接受正则表达式和。
在正则表达式中有特殊含义,如果我们想指定实际的。
我们需要对其进行转义,因此我们使用 \\。
。另请注意,在较新版本的 tidyr $中,
gather
已替换为 pivot_longer
。 c $ c>。
Since the sep
argument in separate
accepts regex and .
has a special meaning in regex, if we want to specify actual .
we need to escape it, hence we use "\\."
. Also note that gather
has been replaced with pivot_longer
in the newer version of tidyr
.
这篇关于“”的含义是什么? &。”在tidyr :: separate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!