先设置一个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所有行中循环遍历。
原文:http://www.cnblogs.com/xugege/p/7357172.html