R语言学习-for循环

先设置一个Data Frame

df = data.frame(age=c(21, 22, 23),name=c('KEN', 'John', 'JIMI'),stringsAsFactors = FALSE);

输出df如下

age name
1 21 KEN
2 22 John
3 23 JIMI

通过for循环来输出

for(i in 1:nrow(df)){
  l<-df[i,]  
  print(l)
  print(l['age'])
}

其中,nrow(df)计算出df中行的数量,然后i在df所有行中循环遍历。

posted @ 2017-08-14 12:42  绪哥哥  阅读(902)  评论(0编辑  收藏  举报