Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-当分隔符为点时,将一列拆分为两列_R - Fatal编程技术网

R-当分隔符为点时,将一列拆分为两列

R-当分隔符为点时,将一列拆分为两列,r,R,当分隔符是点时,我试图将一列拆分为两个单独的列 接下来,我首先尝试: 库(tidyverse) df1% as.character%>% str_split_fixed(“.”,2)%>% 头 #> [,1] [,2] #> [1,] "" ".1" #> [2,] "" ".1" #> [3,] "" ".1" 由(v0.3.0)于2021-04-05创建 但这不是我想要的:第一列是空的,第二列仍然有点 根据我在上面链接的帖子中的评论,我尝试添加fixed=“.”或fi

当分隔符是点时,我试图将一列拆分为两个单独的列

接下来,我首先尝试:

库(tidyverse)
df1%
as.character%>%
str_split_fixed(“.”,2)%>%
头
#>      [,1] [,2]
#> [1,] ""   ".1"
#> [2,] ""   ".1"
#> [3,] ""   ".1"
由(v0.3.0)于2021-04-05创建

但这不是我想要的:第一列是空的,第二列仍然有点

根据我在上面链接的帖子中的评论,我尝试添加
fixed=“.”
fixed=TRUE
,但似乎不起作用:

库(tidyverse)
df1%
as.character%>%
str_split_fixed(“.”,fixed=“.”,2)%
头
#>str_split_fixed(,“,”fixed=“”,2)中出错:未使用的参数(fixed=“.”)
由(v0.3.0)创建于2021-04-05

df1 %>% separate(Sequence.Name, into = c("Col1", "Col2"))

  Col1 Col2 var1
1    2    1    1
2    2    1    1
3    2    1    0

也可以在
base R
中使用
read.table

cbind(read.table(text = as.character(df1$Sequence.Name), sep=".", 
         header = FALSE, col.names = c("Col1", "Col2")), df1['var1'])
#  Col1 Col2 var1
#1    2    1    1
#2    2    1    1
#3    2    1    0

下面是一个使用
tstrsplit

> setDT(df1)[, c(lapply(tstrsplit(Sequence.Name, "\\."), as.numeric), .(var1))]
   V1 V2 V3
1:  2  1  1
2:  2  1  1
3:  2  1  0
这也有助于:

library(tidyr)

df1 %>%
  extract(col = Sequence.Name, into = c("Sequence", "Name"), regex = "(.).(.)")

  Sequence Name var1
1        2    1    1
2        2    1    1
3        2    1    0


使用
read.table
是多么聪明的方法啊!