在R中一次跨10列循环 [英] Looping across 10 columns at a time in R

查看:94
本文介绍了在R中一次跨10列循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含1000列的数据框.我试图一次循环遍历10列,并使用TraMineR包中的seqdef()函数对这些列中的数据进行序列比对.因此,我想将此功能应用于第一个复行中的第1-10列,以及第二个复行中的第11-20列,一直到1000.

I have a dataframe with 1000 columns. I am trying to loop over 10 columns at a time and use the seqdef() function from the TraMineR package to do sequence alignment across the data in those columns. Hence, I want to apply this function to columns 1-10 in the first go-around, and columns 11-20 in the second go-around, all the way up to 1000.

这是我正在使用的代码.

This is the code I am using.

library(TraMineR)
by(df[, 1:10], seqdef(df))

但是,这只会在前10个循环,然后停止.如何在10列的块之间循环?

However, this only loops over the first 10 and then stops. How do I loop it across chunks of 10 columns?

推荐答案

您可以使用mapply并设置要遍历的列顺序来执行类似的操作.

You could do something like this using mapply and setting the sequence of columns to loop across.

colpicks <- seq(10,1000,by=10)
mapply(function(start,stop) seqdef(df[,start:stop]), colpicks-9, colpicks)

这篇关于在R中一次跨10列循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆