Reputation: 2443
target<-data.frame(q01=1,q03=1:10)
total<-list(t1=c('q02','q05'),t2=c('q01','q04'),t3=c('q03','q06'))
for(m in colnames(target)){
for(j in total){
print(names(j))
}
}
When I run above script,result is:
NULL
NULL
NULL
NULL
NULL
NULL
My expect result is:
t1
t2
t3
t1
t2
t3
I cannot find out the problem,names(j)
seemed not workable in loop.
Where is the problem?
Upvotes: 0
Views: 36
Reputation: 389235
Iterate over the index of the list instead of the entire list :
for(m in colnames(target)){
for(j in seq_along(total)) {
print(names(total)[j])
}
}
#[1] "t1"
#[1] "t2"
#[1] "t3"
#[1] "t1"
#[1] "t2"
#[1] "t3"
Upvotes: 1