mercredi 22 août 2018

Creating a loop to add labels to colums: library(Hmisc)

I have a dataset which looks something like this:

  Year      Country  Matchcode  P             H
1 2000      France        0001  1213          1872     
2 2001      France        0002  1234          2345      
3 2000      UK            0003  1726          2234      
4 2001      UK            0004  6433          9082  

I have another dataset which looks something like this:

P    Power
H    Happiness

I would like to add info in the second column of the second dataset (Power, Happiness) to the abbreviation used in the first dataset with a loop, but I don't know exactly how to write the loop.

This is how far I got:

library(Hmisc)
for i in df2[,1]{
if (df1[,i] == df2[i,]){
label(df1[,i]) <- df2[i,2]
}}

But this merely checks whether names are the same and does not search for it. Could anyone guide further?

Desired output:

  Year      Country  Matchcode  P(label=Power) H(label=Happiness)
1 2000      France        0001            1213              1872     
2 2001      France        0002            1234              2345      
3 2000      UK            0003            1726              2234      
4 2001      UK            0004            6433              9082  

Aucun commentaire:

Enregistrer un commentaire