Create one column indexed to another in R

3

Good afternoon!

I'm new to R and I have a database where I need to include a column, though the values in this column need to be tied to the values in another column.

In this case, I have the ano (2006: 2016) column and need to create the PIB column (with specific values for each year).

Years do not follow the sequence, some jump.

I'm using dCoopCred$ano and dCoopCred$PIB .

Example:

ano     PIB
2006    4,0
2007    6,1
2008    5,1
2009    -0,1
2010    7,5
2011    4,0
2012    1,9
2013    3,0
2014    0,5
2015    -3,8
2016    -3,6

However, there are 10 thousand lines, the years do not follow a sequence.

Sorry for anything, it's my first post here.

Thank you in advance.

    
asked by anonymous 14.03.2018 / 19:50

1 answer

2

I solved by a for:

dCoopCred$PIB <- c(0) for(i in row(dCoopCred)){  
if(dCoopCred$ano[i]==2006){dCoopCred$PIB[i] <- c(4)     
} else if (dCoopCred$ano[i]==2007){dCoopCred$PIB[i] <- c(6.1)   
 }else if (dCoopCred$ano[i]==2008){dCoopCred$PIB[i] <- c(5.1)   
}else if (dCoopCred$ano[i]==2009){dCoopCred$PIB[i] <- c(-0.1)   
}else if (dCoopCred$ano[i]==2010){dCoopCred$PIB[i] <- c(7.5) 
}....
}}
    
05.04.2018 / 05:02