mardi 23 juillet 2019

How to apply if-else function for multiples "if statement" and two different dataframes sizes?

I have two dataframes of different sizes:

`> df1 <- dput(head(public_t))
 structure(list(zona = c(1, 1, 1, 1, 1, 1), renda_fa = c(2732.58, 
 3579.58, 3579.58, 2323.9, 2323.9, 6000), decile_rank = c(3L, 
 4L, 4L, 2L, 2L, 7L), distancia = c(4260.03, 3173.1, 3173.1, 
 21562.81, 
 21562.81, 2696.04)), class = c("rowwise_df", "tbl_df", "tbl", 
 "data.frame"), row.names = c(NA, -6L), .Names = c("zona", 
 "renda_fa", 
 "decile_rank", "distancia"))`

`> df2 <- dput(head(cost))
 structure(list(decile = c(1, 2, 3, 4, 5, 6), cost_tpublic = 
 c(10.5, 
 7.1, 6.1, 5.6, 4.8, 4.2), cost_tprivate = c(11.6, 10.6, 10.2, 
 11.5, 12.4, 12.5)), .Names = c("decile", "cost_tpublic", 
 "cost_tprivate"
 ), row.names = c(NA, 6L), class = "data.frame")`

I need to return the value from df2$cost_tpublic correspondent to df1$decile_rank, in a new column in df1.

How might I do that? Thank you!

I would expect something like this

`> df1 <- dput(head(public_t))
structure(list(zona = c(1, 1, 1, 1, 1, 1), renda_fa = c(2732.58, 
3579.58, 3579.58, 2323.9, 2323.9, 6000), decile_rank = c(3L, 
4L, 4L, 2L, 2L, 7L), distancia = c(4260.03, 3173.1, 3173.1, 
21562.81, 
21562.81, 2696.04)), **percentage_income = c(5.6, 4.8, 4.8, 7.1, 
7.1, NA)**, class = c("rowwise_df", "tbl_df", "tbl", "tbl", 
"data.frame"), row.names = c(NA, -6L), .Names = c("zona", 
"renda_fa", 
"decile_rank", "distancia", "percentage_income"))`

Aucun commentaire:

Enregistrer un commentaire