由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - 请教在R里面如何拆character string.
相关主题
怎样用R subset character stringbatch?
问一个用R计算年龄的问题Anyone has SAS programmer standard working procedure?
R data.frame如何获得最后的几个数字
一个用R 进行data preparation的问题弱问大数据处理
再问一个R问题SAS regex questions
请问R:如何把data frame变成一列数报个Intuit的offer (转载)
How can I do this in R?[转载] Re: SAS plot problem
问一下R的读取数据问题What is wrong with following code?
相关话题的讨论汇总
话题: strsplit话题: character话题: nc话题: unlist话题: your
进入Statistics版参与讨论
1 (共1页)
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
2
strsplit(x, "[ \n]")
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"

1 (共1页)
进入Statistics版参与讨论
相关主题
What is wrong with following code? 再问一个R问题
another sas question请问R:如何把data frame变成一列数
[合集] sas 的一个问题How can I do this in R?
[合集] 请教Excel 操作 (Excel 2007)问一下R的读取数据问题
怎样用R subset character stringbatch?
问一个用R计算年龄的问题Anyone has SAS programmer standard working procedure?
R data.frame如何获得最后的几个数字
一个用R 进行data preparation的问题弱问大数据处理
相关话题的讨论汇总
话题: strsplit话题: character话题: nc话题: unlist话题: your