如何获取一个字符值列表和一个数字值列表并将这些字符用作标签。就像如果我有一个由30个数字和10个字符组成的列表一样,如何将这些字符以等间距插入数字列表中?
a
b
c
1
2
3
4
5
6
7
8
9
将其转换为:
a
1
2
3
b
1
...
输入字母的格式如下:
c("a", "b", "c")
和数字:
"x"
"1" 0.56
"x"
"1" 0.45
"x"
"1" 0.44
"x"
"1" 0.67
"x"
"1" 0.29
"x"
"1" 0.02
"x"
"1" 0.13
"x"
"1" 0.15
答案 0 :(得分:1)
正如我在评论中提到的,我不清楚您想要输入和输出的数据类型。我在这里猜测:
c <-c("a","b","c") # some character
n <- c(1,2,3,4 ,5,6,7,8,9) # some numeric
m <- 1
for(i in 1:length(c)){
print(c[i])
print(n[m]); m <- m+1
print(n[m]); m <- m+1
}
[1] "a" [1] 1 [1] 2 [1] "b" [1] 3 [1] 4 [1] "c" [1] 5 [1] 6
即
m <- 1
a <- character()
for(i in 1:length(c)){
a <- c(a,(c[i]))
a <- c(a,n[m]); m <- m+1
a <- c(a,n[m]); m <- m+1
}
a
> a [1] "a" "1" "2" "b" "3" "4" "c" "5" "6"
答案 1 :(得分:0)
我们可以构建矩阵并将其解包:
x <- letters[1:3]
y <- 1:9
c(rbind(x,matrix(y,nrow=length(x))))
# [1] "a" "1" "2" "3" "b" "4" "5" "6" "c" "7" "8" "9"
或者我们可以使用split<-
:
lx <- length(x)
ly <- length(y)
res <- character(lx+ly)
split(res,c(1,rep(2,ly/lx))) <- list(x,y)
res
# [1] "a" "1" "2" "3" "b" "4" "5" "6" "c" "7" "8" "9"
c(1,rep(2,ly/lx))
正在构建将被回收的因素,然后将原始值分配给它们的新位置。