index:
store:
type: memory
这给出了包含1,2 ... c
的a1,a2 ... ac变量如何根据b的值使用cbind?例如,请执行以下操作:
index:
store:
type: niofs
c <- readline(prompt="Enter an integer: ")
b <- readline(prompt="Enter an integer: ")
for(i in 1:c){
assign(paste("a", i, sep = ""), i)
}
预期的t1值:
# assume b = 3 and c = 12:
t1 <- cbind(a1,a2,a3)
t2 <- cbind(a4,a5,a6)
t3 <- cbind(a7,a8,a9)
t4 <- cbind(a10,a11,a12)
# assume b = 4 and c = 12:
t1 <- cbind(a1,a2,a3,a4)
t2 <- cbind(a5,a6,a7,a8)
t3 <- cbind(a9,a10,a11,a12)
答案 0 :(得分:0)
我正在对您的数据做出一些假设。我假设您已经为要尝试cbind的列分配了值。
<audio>
<强>输出:强>
a <- 12
b <- 3
test <- NULL
index <- NULL
for(i in 1:a){
test[i] <- paste0("n_", i)
index[i] <- paste(i)
}
start <- seq(1,a-b+1, by=b)
end <- seq(b,a, by=b)
s = list()
k=1
for(k in 1:length(start)){
cbind_list <- start[k]:end[k]
s[[k]] <- rbind(test[seq(cbind_list[1],cbind_list[length(cbind_list)],by=1)])
}
list_cols <- do.call(rbind, s)
n_1 <- rep(1,4)
n_2 <- rep(2,4)
n_3 <- rep(3,4)
n_4 <- rep(4,4)
n_5 <- rep(5,4)
n_6 <- rep(6,4)
n_7 <- rep(7,4)
n_8 <- rep(8,4)
n_9 <- rep(9,4)
n_10 <- rep(10,4)
n_11 <- rep(11,4)
n_12 <- rep(12,4)
df <- data.frame(n_1,n_2,n_3,n_4,n_5,n_6,n_7,n_8,n_9,n_10,n_11,n_12)
t=list()
for(p in 1:nrow(list_cols)){
nam <- paste0("t",p)
assign(nam,cbind(df[,match(list_cols[p,], colnames(df))]))
}
<强>更新:强>
> t1
n_1 n_2 n_3
1 1 2 3
2 1 2 3
3 1 2 3
4 1 2 3
输出:
a <- 6
b <- 3
test <- NULL
index <- NULL
for(i in 1:a){
test[i] <- paste0("n_", i)
index[i] <- paste(i)
}
start <- seq(1,a-b+1, by=b)
end <- seq(b,a, by=b)
s = list()
k=1
for(k in 1:length(start)){
cbind_list <- start[k]:end[k]
s[[k]] <- rbind(test[seq(cbind_list[1],cbind_list[length(cbind_list)],by=1)])
}
list_cols <- do.call(rbind, s)
n_1 <- c(3,5,2)
n_2 <- c(4,7,3)
n_3 <- c(3,5,2)
n_4 <- c(4,5,3)
n_5 <- c(5,5,5)
n_6 <- c(4,3,1)
df <- data.frame(n_1,n_2,n_3,n_4,n_5,n_6)
t=list()
p=1
for(p in 1:nrow(list_cols)){
nam <- paste0("t",p)
assign(nam,cbind(df[,match(list_cols[p,], colnames(df))]))
}