两个可变循环“for”以正确的顺序输出(two variable loop “for” output in the right sequence)

编程入门 行业动态 更新时间:2024-10-26 01:19:46
两个可变循环“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) } } c

The 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) }

更多推荐

本文发布于:2023-08-03 00:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382458.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   正确   两个   variable   output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!