两个可变循环“for”以正确的顺序输出(two variable loop “for” output in the right sequence)
我怎样才能使这个循环,以与数据框(a,b)相同的顺序出现。
a <- seq(1,36,2) b <- seq(2,36,2) c <- NULL for (i in a) { for (j in b) { d <- cbind(i, j) c <- rbind(c,d) } } c输出c以我想要的顺序给出j ,但是我每18次重复一次。
这是我想要的方式
data.frame(a,b)How can I make this loop, appearing in the same sequence as dataframe(a,b).
a <- seq(1,36,2) b <- seq(2,36,2) c <- NULL for (i in a) { for (j in b) { d <- cbind(i, j) c <- rbind(c,d) } } cThe output c gives j in the order I want, but i repeats itself every 18 times.
This is the way I want it
data.frame(a,b)最满意答案
你不需要两个循环,只需要一个循环。
a <- seq(1,36,2) b <- seq(2,36,2) c <- NULL for(i in 1:length(a)){ d<-cbind(a[i],b[i]) c<-rbind(c,d) }You don't need two loops, just one.
a <- seq(1,36,2) b <- seq(2,36,2) c <- NULL for(i in 1:length(a)){ d<-cbind(a[i],b[i]) c<-rbind(c,d) }更多推荐
发布评论