Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_Dataframe - Fatal编程技术网

R 拆分字符串并重新排列数据帧

R 拆分字符串并重新排列数据帧,r,string,dataframe,R,String,Dataframe,我有这样的数据 df <- structure(list(A = structure(c(2L, 3L, 6L, 7L, 5L, 4L, 1L, 1L ), .Label = c("", "NZT1", "O749", "P42I;QJ0;AIH2", "P609;QT7", "Q835", "Q854"), class = "factor"), B = structure(c(8L, 6L, 5L, 7L, 4L, 3L, 2L, 1L), .Label = c("", "P079;

我有这样的数据

df <- structure(list(A = structure(c(2L, 3L, 6L, 7L, 5L, 4L, 1L, 1L
), .Label = c("", "NZT1", "O749", "P42I;QJ0;AIH2", "P609;QT7", 
"Q835", "Q854"), class = "factor"), B = structure(c(8L, 6L, 5L, 
7L, 4L, 3L, 2L, 1L), .Label = c("", "P079;P0C7;P0C8", "P641;Q614", 
"Q013", "Q554", "Q749", "Q955", "Q9U0"), class = "factor"), C = structure(c(7L, 
8L, 6L, 5L, 3L, 4L, 1L, 2L), .Label = c("P641;QS14", "P679;P0C7;P048", 
"Q168", "Q413", "Q550", "Q6N9", "Q980", "Q997"), class = "factor")), .Names = c("A", 
"B", "C"), class = "data.frame", row.names = c(NA, -8L))

#              A              B              C
#1          NZT1           Q9U0           Q980
#2          O749           Q749           Q997
#3          Q835           Q554           Q6N9
#4          Q854           Q955           Q550
#5      P609;QT7           Q013           Q168
#6 P42I;QJ0;AIH2      P641;Q614           Q413
#7               P079;P0C7;P0C8      P641;QS14
#8                              P679;P0C7;P048
我试着使用strsplit(),但没能做到这一点

这就是我试过的

myNewdf <- strsplit(as.character(unlist(df)), ";")

myNewdf我想你可以试试这个:

x <- lapply(df, function (x) unlist(strsplit(as.character(x), ";")))

如果每列中的项目数不相同,
扫描
功能将在此处成功,尽管
as.data.frame
将阻塞:

as.data.frame(lapply( df, function(x) scan( text=as.character(x) , what="", sep=";", blank.lines.skip = FALSE))
+ )
Read 11 items
Read 11 items
Read 11 items
      A    B    C
1  NZT1 Q9U0 Q980
2  O749 Q749 Q997
3  Q835 Q554 Q6N9
4  Q854 Q955 Q550
5  P609 Q013 Q168
6   QT7 P641 Q413
7  P42I Q614 P641
8   QJ0 P079 QS14
9  AIH2 P0C7 P679
10      P0C8 P0C7
11           P048

或使用
ts
功能:

lst <- lapply(df, function(a) unlist(strsplit(as.character(a), split = ";"))) # 1
tsr <- cbind(ts(lst$A), ts(lst$B), ts(lst$C)) # 2
tsr[is.na(tsr)] <- "" # 3
newDF <- as.data.frame(tsr) # 4
colnames(newDF) <- colnames(df) # 5 (if needed)

      # A    B    C
# 1  NZT1 Q9U0 Q980
# 2  O749 Q749 Q997
# 3  Q835 Q554 Q6N9
# 4  Q854 Q955 Q550
# 5  P609 Q013 Q168
# 6   QT7 P641 Q413
# 7  P42I Q614 P641
# 8   QJ0 P079 QS14
# 9  AIH2 P0C7 P679
# 10      P0C8 P0C7
# 11           P048

lst这里是另一个带有
stri_list2matrix
的选项。这将返回一个
矩阵
,其中NA为缺失值。如果需要
'
,请使用
stri_list2matrix
中的
fill='
参数。此外,可以使用
as.data.frame
将其转换为
data.frame

 library(stringi)
 stri_list2matrix(lapply(df, function(x) unlist(strsplit(as.character(x), ";"))))

scan
实际上是所有
read.*
功能的核心。这是一个低级函数,但它可以执行其他任务,如对
what
参数使用适当的参数进行mulit-line读取。实际上,使用它读取单个向量,使用策略有许多SO和Rhelp示例。我是从G.Grothendieck的答案中学到的。在过去,我们给
scan
read.*
函数一个
textConnection()
参数,你可能仍然需要用
readLines
这样做,因为它不是基于
scan
。我想我已经讲清楚了这一点。您需要构造另一个方法,用rep(“,length-max.length”)填充较短的项目。
textConnection
非常简单。它只是把一个向量转换成大多数函数都会看到的文件。试一试:
x@nik由于问题被搁置,其他人无法添加答案。我们将等待此人(拖延者)回复您的评论。
stri_list2matrix
将给出一个字符矩阵,其中
NA
为缺失值。这就是OP所要求的吗?@m0h3n您可以使用
fill
参数将其更改为
'
。另外,
as.data.frame
可以将其转换为
data.frame
,因此最好在答案中指出它们。我想你会记得你昨天的评论。:-)@李哲远:我接受你的回答,谢谢,但如果你能给你的剧本写一些定义,我会很高兴,这样我就能从中学习
lst <- lapply(df, function(a) unlist(strsplit(as.character(a), split = ";"))) # 1
tsr <- cbind(ts(lst$A), ts(lst$B), ts(lst$C)) # 2
tsr[is.na(tsr)] <- "" # 3
newDF <- as.data.frame(tsr) # 4
colnames(newDF) <- colnames(df) # 5 (if needed)

      # A    B    C
# 1  NZT1 Q9U0 Q980
# 2  O749 Q749 Q997
# 3  Q835 Q554 Q6N9
# 4  Q854 Q955 Q550
# 5  P609 Q013 Q168
# 6   QT7 P641 Q413
# 7  P42I Q614 P641
# 8   QJ0 P079 QS14
# 9  AIH2 P0C7 P679
# 10      P0C8 P0C7
# 11           P048
 library(stringi)
 stri_list2matrix(lapply(df, function(x) unlist(strsplit(as.character(x), ";"))))