如何提取R'的结果;s strsplit()
问题: 我有一个字符串向量foo:如何提取R'的结果;s strsplit(),r,strsplit,R,Strsplit,问题: 我有一个字符串向量foo: >foo = c("1x2","3x4","5x6","7x8","9x10") 我将单个字符串拆分到“x”上,并将结果粘贴到goo中: >goo = strsplit(foo, "x") >goo [[1]] [1] "1" "2" [[2]] [1] "3" "4" [[3]] [1] "5" "6" [[4]] [1] "7" "8" [[5]] [1] "9" "10" 如何从列表中提取第一列和第二列?(我想要(1,3,
>foo = c("1x2","3x4","5x6","7x8","9x10")
我将单个字符串拆分到“x”上,并将结果粘贴到goo中:
>goo = strsplit(foo, "x")
>goo
[[1]]
[1] "1" "2"
[[2]]
[1] "3" "4"
[[3]]
[1] "5" "6"
[[4]]
[1] "7" "8"
[[5]]
[1] "9" "10"
如何从列表中提取第一列和第二列?(我想要(1,3,5,7,9)和(2,4,6,8,10))使用
sapply
使用“[]”连续“提取”:
我一直认为这应该是结果,但我可能不理解问题所在。使用
sapply
使用“[[””连续“提取”:
我一直认为这应该是结果,但我可能不理解问题所在。最简单的方法是将结果包装在一个
精辟的语句中
odd_results <- sapply(goo, function(x) x[1])
odd_results最简单的方法是将结果包装在sapply
语句中
odd_results <- sapply(goo, function(x) x[1])
odd_结果概述
您需要指定按位置提取列表元素
# load data
foo <- c("1x2","3x4","5x6","7x8","9x10")
# split foo by 'x'
foo.list <- strsplit( x = foo, split = "x", fixed = TRUE )
# store the first and second elements of each list in a data frame
foo.df <-
data.frame(
First_Element = unlist( lapply( X = foo.list, FUN = "[[", FUN.VALUE = 1 ) )
, Second_Element = unlist( lapply( X = foo.list, FUN = "[[", FUN.VALUE = 2 ) )
)
#加载数据
foo概述
您需要指定按位置提取列表元素
# load data
foo <- c("1x2","3x4","5x6","7x8","9x10")
# split foo by 'x'
foo.list <- strsplit( x = foo, split = "x", fixed = TRUE )
# store the first and second elements of each list in a data frame
foo.df <-
data.frame(
First_Element = unlist( lapply( X = foo.list, FUN = "[[", FUN.VALUE = 1 ) )
, Second_Element = unlist( lapply( X = foo.list, FUN = "[[", FUN.VALUE = 2 ) )
)
#加载数据
foo为了多样性,你可以使用
goo <- unlist(strsplit(foo, "x"))
a <- goo[seq(1, length(goo), 2)]
b <- goo[seq(2, length(goo), 2)]
及
分别。为了多样性,您可以使用
goo <- unlist(strsplit(foo, "x"))
a <- goo[seq(1, length(goo), 2)]
b <- goo[seq(2, length(goo), 2)]
> result <- do.call(rbind, goo)
> result
[,1] [,2]
[1,] "1" "2"
[2,] "3" "4"
[3,] "5" "6"
[4,] "7" "8"
[5,] "9" "10"
> result[, 1] # column 1
[1] "1" "3" "5" "7" "9"
> result[, 2] # column 2
[1] "2" "4" "6" "8" "10"
及
分别。>结果
> result <- do.call(rbind, goo)
> result
[,1] [,2]
[1,] "1" "2"
[2,] "3" "4"
[3,] "5" "6"
[4,] "7" "8"
[5,] "9" "10"
> result[, 1] # column 1
[1] "1" "3" "5" "7" "9"
> result[, 2] # column 2
[1] "2" "4" "6" "8" "10"
[,1] [,2]
[1,] "1" "2"
[2,] "3" "4"
[3,] "5" "6"
[4,] "7" "8"
[5,] "9" "10"
>结果[,1]#第1列
[1] "1" "3" "5" "7" "9"
>结果[,2]#第2列
[1] "2" "4" "6" "8" "10"
>结果
[,1] [,2]
[1,] "1" "2"
[2,] "3" "4"
[3,] "5" "6"
[4,] "7" "8"
[5,] "9" "10"
>结果[,1]#第1列
[1] "1" "3" "5" "7" "9"
>结果[,2]#第2列
[1] "2" "4" "6" "8" "10"
sappy(goo,identity)
然后切片行。read.table(text=foo,sep='x')[,1]
sappy(goo,identity)
然后切片行。read.table(text=foo,sep='x')[,1]