0

11 つの変数 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, "\\(")

ID1_s1 と ID_s2 "(" ブラケットに基づいて ID1 を分離するために strplit を実行するにはどうすればよいですか?

次のような出力が必要です。

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

3 に答える 3

1
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
于 2015-05-27T02:53:12.400 に答える
1

stringsAsFactors = FALSEデータ フレームを定義するときに使用します (または、既に存在するdf1 <- transform(df1, ID1 = as.character(df1))場合は、指定されたパターンをstrsplit.

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"

質問が更新された後に追加され、目的の出力が含まれるようになりました。read.patterngsubfn パッケージで使用して、次のようにフィールドを分割します。

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

追加出力に括弧が存在することが重要でない場合、別の解決策は次のとおりです(s上記のコードを使用):

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
于 2015-05-27T00:14:21.260 に答える
0

おやすみなさい、ダミーデータと前に与えられた提案を使用して、期待される結果を生成するために以下のコードを準備 (およびテスト) しました。

皆様のデータ処理の一助になれば幸いです。

# 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
于 2015-05-27T01:02:51.993 に答える