q**j 发帖数: 10612 | 1 比如
x = as.character(a b c d)
y= strsplit(x, " ")就可以把它拆成a, b, c,d。
可是如果
x = as.character(a b\nc d)
就不灵了。这里面b和c之间用回车隔开的。
请教如果这样,应该如何应付?
多谢。 |
t**i 发帖数: 688 | |
t**i 发帖数: 688 | 3 BTW, It should be as.character("a b\nc d"). The code as you put does not
work. |
q**j 发帖数: 10612 | 4 you are right. i neglected this detail.
thanks a lot for the help. But can your code cannot break blank now. Can we break the two at the same time?
also why do we need a [] to enclose \n?
【在 t**i 的大作中提到】 : BTW, It should be as.character("a b\nc d"). The code as you put does not : work.
|
t**i 发帖数: 688 | 5 I believe my code can break space together with \n. Make sure you used the
exact token as I typed :-) |
R*********r 发帖数: 225 | 6 你这个好像拆成"ab","cd"了,呵呵。
>unlist(strsplit(unlist(strsplit("a b\nc d",split="\n")),split=' '))
[1] "a" "b" "c" "d"
the
【在 t**i 的大作中提到】 : I believe my code can break space together with \n. Make sure you used the : exact token as I typed :-)
|
t**i 发帖数: 688 | 7 If you look closely, you should notice that I used regex for the splitter.
That is, either space or \n will be recognized. I tried the following and
it worked. Please do copy and paste to make sure you do not miss the
invisible space character.
> x="a b\nc d"
> strsplit(x,"[ \n]")
[[1]]
[1] "a" "b" "c" "d" |
q**j 发帖数: 10612 | 8 This is great. I am not an expert here. Thanks a lot for pointing this out.
【在 t**i 的大作中提到】 : If you look closely, you should notice that I used regex for the splitter. : That is, either space or \n will be recognized. I tried the following and : it worked. Please do copy and paste to make sure you do not miss the : invisible space character. : > x="a b\nc d" : > strsplit(x,"[ \n]") : [[1]] : [1] "a" "b" "c" "d"
|
q**j 发帖数: 10612 | 9 Thanks a lot. I tried your method. But I still find it is not perfect.
if x = "a b\n c d", the code will give an extra blank element. The reason is
that your code will break either \n or " ", as you said. But when both are
present, it breaks only one of them. In this case, it is "\n".
Your suggestions have been very helpful. Any further thoughts?
【在 t**i 的大作中提到】 : If you look closely, you should notice that I used regex for the splitter. : That is, either space or \n will be recognized. I tried the following and : it worked. Please do copy and paste to make sure you do not miss the : invisible space character. : > x="a b\nc d" : > strsplit(x,"[ \n]") : [[1]] : [1] "a" "b" "c" "d"
|
t**i 发帖数: 688 | 10 > x="a b\n \tc\r \nd"
> strsplit(x, "[ *|\t*|\r*|\n*|\v*|\f*]+")
[[1]]
[1] "a" "b" "c" "d" |
q**j 发帖数: 10612 | 11 Thanks a lot! Yes, this will work. And I am happy to get to know unlist,
regular expressions and you and tosi. Many thanks. But it is still
interesting to know how to make this work with regular expression.
【在 R*********r 的大作中提到】 : 你这个好像拆成"ab","cd"了,呵呵。 : >unlist(strsplit(unlist(strsplit("a b\nc d",split="\n")),split=' ')) : [1] "a" "b" "c" "d" : : the
|
q**j 发帖数: 10612 | 12 Get it and it works beautifully.
【在 t**i 的大作中提到】 : > x="a b\n \tc\r \nd" : > strsplit(x, "[ *|\t*|\r*|\n*|\v*|\f*]+") : [[1]] : [1] "a" "b" "c" "d"
|