jeudi 5 mars 2015

Fill a dataframe in a for-loop in R


Vote count:

0




I'm a total newbie at R, and probably am making a silly mistake, but I've been stuck on this for hours now. I have a dataframe (countries.averages) with row/column titles and the correct size filled with NA's that I am trying to fill using a for loop. Country.names is a large dataset that looks something like this.



A B C D
A 1 3 3 4
B 2 1 3 4
A 4 2 1 3
C 3 2 2 1


Here's the code:



num.countries<-4
for (i in num.countries){
for (j in num.countries){
x<-paste0(country.names[i])
y<-paste0(country.names[j])
z<-subset(Voting.50.50,Country==x)
country.average[i,j]<-mean(z[,j],na.rm=TRUE)
}
}


The problem is with writing the dataframe, country.averages[i,j]. I know this is probably not correct, but I have no idea why, or what else to do. I'm not sure of the correct terminology (sorry!), but is there some other way to write a value to a specific cell in a dataframe?



asked 44 secs ago







Fill a dataframe in a for-loop in R

Aucun commentaire:

Enregistrer un commentaire