strsplit符号“|”导致 R 中的非字符错误消息
非常感谢您的帮助。
是的。我应该提供一个更好的例子。
这是我的输入文件 (3columns.csv)
Patients Markers Studies
1 AA EXX 1111
2 BB ABCB1 2222|3333|5555|6666
3 CC CCAN 4444|5555
4 DD ABCB1 6666
这是我的输出文件
Patients Markers Studies
1 AA EXX 1111
2 BB ABCB1 2222
2 BB ABCB1 3333
2 BB ABCB1 5555
2 BB ABCB1 6666
3 CC CCAN 4444
3 CC CCAN 5555
4 DD ABCB1 6666
(1) 根据下面的命令,我对第 6 行进行了一些更改,如下
sapply(unlist(strsplit(as.character(df[x,3]),"\\|")),c,df[x,1:2],USE.NAMES=FALSE)
(2) 我尝试将 df 文件调用为
df <- read.csv(file="3columns.csv",header=TRUE,stringsAsFactors=FALSE)
(3)我还尝试在 |
之前添加 \\
所有这些方法都不起作用,所以我怀疑我可能误解了下面的回复。您介意给我更多指导吗?
此致, Catherine
------原始问题--------------------------
我想使用 R 的 strsplit
命令根据符号“|”分隔单元格。
但是,出现错误消息:
Error in strsplit(df[x, 3], "|") : non-character argument.
此错误消息是什么意思?
我该如何纠正这个错误?
我使用的是本网站上一个问题中列出的命令行:
> write.csv(df, file="3columns.csv")
> as.data.frame(
+ t(
+ do.call(cbind,
+ lapply(1:nrow(df),function(x){
+ sapply(unlist(strsplit(df[x,3],"|")),c,df[x,1:2],USE.NAMES=FALSE)
+ })
+ )
+ )
+ )
Thanks you very much for your help.
Yes. I should provide a better example.
Here is my input file (3columns.csv)
Patients Markers Studies
1 AA EXX 1111
2 BB ABCB1 2222|3333|5555|6666
3 CC CCAN 4444|5555
4 DD ABCB1 6666
Here is my output file
Patients Markers Studies
1 AA EXX 1111
2 BB ABCB1 2222
2 BB ABCB1 3333
2 BB ABCB1 5555
2 BB ABCB1 6666
3 CC CCAN 4444
3 CC CCAN 5555
4 DD ABCB1 6666
(1) According to the commands belows, i have made some change to the 6th line as follows
sapply(unlist(strsplit(as.character(df[x,3]),"\\|")),c,df[x,1:2],USE.NAMES=FALSE)
(2) I tried to call up the df file as
df <- read.csv(file="3columns.csv",header=TRUE,stringsAsFactors=FALSE)
(3) I also tried to add \\
before |
All these methods did not work, so I suspect I may have misunderstand the reply below. Could you mind to give me some more guidances?
best regards,
Catherine
------Original Question--------------------------
I want to use R's strsplit
command to separate the cells based on the symbol "|"
.
However, an error message appears:
Error in strsplit(df[x, 3], "|") : non-character argument.
What does this error message mean?
How can I correct this error?
I was using the command line listed in a previous question on this website:
> write.csv(df, file="3columns.csv")
> as.data.frame(
+ t(
+ do.call(cbind,
+ lapply(1:nrow(df),function(x){
+ sapply(unlist(strsplit(df[x,3],"|")),c,df[x,1:2],USE.NAMES=FALSE)
+ })
+ )
+ )
+ )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有最小的可重现示例,很难看出到底出了什么问题。但是
strsplit(df[x, 3], "|")
不起作用,因为|
符号是字符中的特殊情况(or 的正则表达式)。你实际上需要双重转义:It is hard to see what is actually going wrong without a minimal reproducible example. But
strsplit(df[x, 3], "|")
would not work since the|
sign is a special case in characters (regular expression for or). You actually need to double escape this:|
是正则表达式中使用的特殊字符。您需要使用\\
转义|
以获得您想要的效果:请参阅
?regex
并搜索“特殊字符”查找整个字符列表。|
is a special character used in regular expressions. You need to escape the|
with\\
in order to get the effect you are after:See
?regex
and search for "special characters" to find the whole list of characters.