在R中分割一个字符串向量。

时间:2022-03-02 21:44:22

I have the following vector:

我有以下向量:

tmp3 <- c("1500 2", "1500 1", "1510 2", "1510 1", "1520 2", "1520 1", "1530 2", 
"1530 1", "1540 2", "1540 1")

I would like to just retain the second number in each of the atoms of this vector, so it would read:

我想在这个向量的每个原子中保留第二个数字,所以它会读到:

c(2,1,2,1,2,1,2,1,2,1)

8 个解决方案

#1


32  

There's probably a better way, but here are two approaches with strsplit():

可能有更好的方法,但这里有两种方法:strsplit():

as.numeric(data.frame(strsplit(tmp3, " "))[2,])
as.numeric(lapply(strsplit(tmp3," "), function(x) x[2]))

The as.numeric() may not be necessary if you can use characters...

如果您可以使用字符,则可能不需要数字()。

#2


21  

One could use read.table on textConnection:

一个可以用阅读。在textConnection表:

X <- read.table(textConnection(tmp3))

then

然后

> str(X)
'data.frame':   10 obs. of  2 variables:
 $ V1: int  1500 1500 1510 1510 1520 1520 1530 1530 1540 1540
 $ V2: int  2 1 2 1 2 1 2 1 2 1

so X$V2 is what you need.

所以X$V2是你需要的。

#3


9  

It depends a little bit on how closely your actual data matches the example data you've given. I you're just trying to get everything after the space, you can use gsub:

这取决于你的实际数据与你给出的示例数据有多接近。我你只是想把所有的东西都做完,你可以用gsub:

gsub(".+\\s+", "", tmp3)
[1] "2" "1" "2" "1" "2" "1" "2" "1" "2" "1"

If you're trying to implement a rule more complicated than "take everything after the space", you'll need a more complicated regular expresion.

如果你想要实现一个比“把所有东西都放进去”更复杂的规则,你需要一个更复杂的正则表达式。

#4


9  

What I think is the most elegant way to do this

我认为这是最优雅的方式!

>     res <- sapply(strsplit(tmp3, " "), "[[", 2)

If you need it to be an integer

如果你需要它是一个整数。

>     storage.mode(res) <- "integer"

#5


5  

substr(x = tmp3, start = 6, stop = 6)

So long as your strings are always the same length, this should do the trick.

只要你的字符串长度相同,这就行了。

(And, of course, you don't have to specify the argument names - substr(tmp3, 6, 6) works fine, too)

(当然,您不必指定参数名——substr(tmp3, 6, 6)也可以正常工作)

#6


4  

This should do it:

这应该这样做:

library(plyr)
ldply(strsplit(tmp3, split = " "))[[2]]

If you need a numeric vector, use

如果您需要一个数字矢量,请使用。

as.numeric(ldply(strsplit(tmp3, split = " "))[[2]])

#7


1  

Another option is scan(). To get the second value, we can use a logical subset.

另一个选择是扫描()。为了获得第二个值,我们可以使用一个逻辑子集。

scan(text = tmp3)[c(FALSE, TRUE)]
#  [1] 2 1 2 1 2 1 2 1 2 1

#8


-1  

An easier way to split 1 column into 2 columns via data.table

一种更简单的方法,将1列分成2列,通过数据表。

require(data.table)  
data_ex = data.table( a = paste( sample(1:3, size=10, replace=TRUE),"-separate", sep="" ))  
data_ex[, number:=  unlist( strsplit(x=a, split="-") )[[1]], by=a]  
data_ex[, word:= unlist( strsplit(x=a, split="-") )[[2]], by=a ]

#1


32  

There's probably a better way, but here are two approaches with strsplit():

可能有更好的方法,但这里有两种方法:strsplit():

as.numeric(data.frame(strsplit(tmp3, " "))[2,])
as.numeric(lapply(strsplit(tmp3," "), function(x) x[2]))

The as.numeric() may not be necessary if you can use characters...

如果您可以使用字符,则可能不需要数字()。

#2


21  

One could use read.table on textConnection:

一个可以用阅读。在textConnection表:

X <- read.table(textConnection(tmp3))

then

然后

> str(X)
'data.frame':   10 obs. of  2 variables:
 $ V1: int  1500 1500 1510 1510 1520 1520 1530 1530 1540 1540
 $ V2: int  2 1 2 1 2 1 2 1 2 1

so X$V2 is what you need.

所以X$V2是你需要的。

#3


9  

It depends a little bit on how closely your actual data matches the example data you've given. I you're just trying to get everything after the space, you can use gsub:

这取决于你的实际数据与你给出的示例数据有多接近。我你只是想把所有的东西都做完,你可以用gsub:

gsub(".+\\s+", "", tmp3)
[1] "2" "1" "2" "1" "2" "1" "2" "1" "2" "1"

If you're trying to implement a rule more complicated than "take everything after the space", you'll need a more complicated regular expresion.

如果你想要实现一个比“把所有东西都放进去”更复杂的规则,你需要一个更复杂的正则表达式。

#4


9  

What I think is the most elegant way to do this

我认为这是最优雅的方式!

>     res <- sapply(strsplit(tmp3, " "), "[[", 2)

If you need it to be an integer

如果你需要它是一个整数。

>     storage.mode(res) <- "integer"

#5


5  

substr(x = tmp3, start = 6, stop = 6)

So long as your strings are always the same length, this should do the trick.

只要你的字符串长度相同,这就行了。

(And, of course, you don't have to specify the argument names - substr(tmp3, 6, 6) works fine, too)

(当然,您不必指定参数名——substr(tmp3, 6, 6)也可以正常工作)

#6


4  

This should do it:

这应该这样做:

library(plyr)
ldply(strsplit(tmp3, split = " "))[[2]]

If you need a numeric vector, use

如果您需要一个数字矢量,请使用。

as.numeric(ldply(strsplit(tmp3, split = " "))[[2]])

#7


1  

Another option is scan(). To get the second value, we can use a logical subset.

另一个选择是扫描()。为了获得第二个值,我们可以使用一个逻辑子集。

scan(text = tmp3)[c(FALSE, TRUE)]
#  [1] 2 1 2 1 2 1 2 1 2 1

#8


-1  

An easier way to split 1 column into 2 columns via data.table

一种更简单的方法,将1列分成2列,通过数据表。

require(data.table)  
data_ex = data.table( a = paste( sample(1:3, size=10, replace=TRUE),"-separate", sep="" ))  
data_ex[, number:=  unlist( strsplit(x=a, split="-") )[[1]], by=a]  
data_ex[, word:= unlist( strsplit(x=a, split="-") )[[2]], by=a ]