1. 程式人生 > >R語言學習-for循環

R語言學習-for循環

als click 遍歷 strings 語言學 www. url str data

先設置一個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所有行中循環遍歷。

R語言學習-for循環