Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.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
具有非字符数据的strsplit_R_Reshape_Reshape2_Strsplit_Agrep - Fatal编程技术网

具有非字符数据的strsplit

具有非字符数据的strsplit,r,reshape,reshape2,strsplit,agrep,R,Reshape,Reshape2,Strsplit,Agrep,1我想对一个变量ID1进行strsplit,将其拆分为ID1_s1和ID1_s2,我需要去掉括号中的字符串 # dummy data df1 <- data.frame(ID1=c("Gindalinc","Xaviertechnolgies","anine.inc(Nasq)","Xyzinc"), y=1:4) strsplit(df1$ID1, "\\(") 定义数据帧时使用stringsAsFactors=FALSE(或者如果数据帧已经存在,使用df1晚安,使用虚拟数据和之前给

1我想对一个变量ID1进行strsplit,将其拆分为ID1_s1和ID1_s2,我需要去掉括号中的字符串

#  dummy data
df1 <- data.frame(ID1=c("Gindalinc","Xaviertechnolgies","anine.inc(Nasq)","Xyzinc"), y=1:4)
strsplit(df1$ID1, "\\(")

定义数据帧时使用
stringsAsFactors=FALSE
(或者如果数据帧已经存在,使用
df1晚安,使用虚拟数据和之前给出的建议,我已经准备(并测试)了下面的代码,以产生预期的结果

我希望它能帮助您处理您的数据

# creating an inicial dataframe
df1 <- data.frame(ID1 = c("Gindalinc","Xaviertechnolgies","anine.inc(Nasq)","Xyzinc"), 
                  y = 1:4, stringsAsFactors = FALSE)

# spliting the element with parenthesis/brackets
y = strsplit(df1$ID1, "[()]")
y

# recreating the parentesis (if needed)
y[[3]][2] = "(Nasq)" 

z = c() # creating null vector for loop

# taking the first element from the list and converting it to a column
for (i in 1:4)  
    z = rbind(z,y[[i]][1])

z2 = c() # creating null vector for loop
# taking the second element from the list and converting it to a column
for (i in 1:4)
    z2 = rbind(z2,y[[i]][2])

# recreating the dataframe in the expected way
df1 = data.frame(ID1_s1 = z,ID1_s2 = z2,y = df1$y)
df1
#创建专用数据帧
df1
library(“tidyr”)

df1您拆分它基于什么-2个新变量中应该包含什么?我希望在“(”上进行拆分ID1中的字符,因此我可以使用ID1_s1和ID_s2。我已更新了我正在查找的拆分模式。ThanksI已添加了我正在查找的输出。您能否建议一个代码来解决此问题?已添加代码以提供添加到问题中的输出。
df1 <- data.frame(ID1 = c("Gindalinc","Xaviertechnolgies","anine.inc(Nasq)","Xyzinc"), 
                  y = 1:4, stringsAsFactors = FALSE)
s <- strsplit(df1$ID1, "[()]")
> s
[[1]]
[1] "Gindalinc"

[[2]]
[1] "Xaviertechnolgies"

[[3]]
[1] "anine.inc" "Nasq"     

[[4]]
[1] "Xyzinc"
library(gsubfn)

cn <- c("ID1_s1", "ID1_s2")
with(df1, data.frame(read.pattern(text = ID1, pattern = "([^(]*)(.*)", col.names = cn), y))

giving:

             ID1_s1 ID1_s2 y
1         Gindalinc        1
2 Xaviertechnolgies        2
3         anine.inc (Nasq) 3
4            Xyzinc        4
data.frame(ID1_s1 = sapply(s, "[", 1), ID1_s2 = sapply(s, "[", 2), y = df1$y)
             ID1_s1 ID1_s2 y
1         Gindalinc   <NA> 1
2 Xaviertechnolgies   <NA> 2
3         anine.inc   Nasq 3
4            Xyzinc   <NA> 4
# creating an inicial dataframe
df1 <- data.frame(ID1 = c("Gindalinc","Xaviertechnolgies","anine.inc(Nasq)","Xyzinc"), 
                  y = 1:4, stringsAsFactors = FALSE)

# spliting the element with parenthesis/brackets
y = strsplit(df1$ID1, "[()]")
y

# recreating the parentesis (if needed)
y[[3]][2] = "(Nasq)" 

z = c() # creating null vector for loop

# taking the first element from the list and converting it to a column
for (i in 1:4)  
    z = rbind(z,y[[i]][1])

z2 = c() # creating null vector for loop
# taking the second element from the list and converting it to a column
for (i in 1:4)
    z2 = rbind(z2,y[[i]][2])

# recreating the dataframe in the expected way
df1 = data.frame(ID1_s1 = z,ID1_s2 = z2,y = df1$y)
df1
library("tidyr")

df1 <- data.frame(ID1=c("Gindalinc","Xaviertechnolgies","anine.inc(Nasq)","Xyzinc"), y=1:4)

df2 <- separate(df1 , ID1 ,c("ID1_s1" , "ID1_s2") , sep = "(?=\\()" , extra = "drop")

#     ID1_s1           ID1_s2  y
# 1  Gindalinc          <NA>   1
# 2 Xaviertechnolgies   <NA>   2
# 3 anine.inc          (Nasq)  3
# 4 Xyzinc              <NA>   4

# if you want to convert na to ""
df2$ID1_s2[is.na(df2$ID1_s2)] <- ""

#         ID1_s1      ID1_s2 y
# 1         Gindalinc        1
# 2 Xaviertechnolgies        2
# 3         anine.inc (Nasq) 3
# 4            Xyzinc        4