Reputation: 1
I am trying to alter the value of a dummy variable for one country for three consecutive years. I have tries doing the following Try 1;
d[14696, "abort"] = 1
Try 2:
d$abort <- ifelse(d$drap == 7.826785, 1, d$abort)
d$abort <- ifelse(d$drap == 8.263874, 1, d$abort)
d$abort <- ifelse(d$drap == 12.060453, 1, d$abort)
Try 3:
d[14698, 6] = 1
Try 4:
d$abort[14698] <- 1
*the number 14698 is the rownumber
None of them gives any errors which makes me assume they work. However, nothing changes in the data set.. Can someone help?
This is what the data frame looks like for Uruguay which needs to be edited for 2016-2019 on the variable abort:
Year | Country | Unemp | Poverty | GDPpc | abort | drap |
---|---|---|---|---|---|---|
2012 | Uruguay | 8.8379 | 0.2 | 12463 | o | 6.425273 |
2013 | Uruguay | 8.3742 | 0.2 | 10849 | 1 | 5.426374 |
2014 | Uruguay | 8.1894 | 0.2 | 12938 | 1 | 6.738383 |
2015 | Uruguay | 8.2349 | 0.3 | 11823 | 1 | 7.377327 |
2016 | Uruguay | 9.2369 | 0.3 | 10828 | o | 7.826785 |
2017 | Uruguay | 9.6273 | 0.4 | 10748 | o | 8.263874 |
2018 | Uruguay | 8.2374 | 0.3 | 11038 | o | 12.060453 |
Upvotes: 0
Views: 384
Reputation: 1
I found out through the help of my professor that i had to load my dataset in a different way:
d <- d[,c("year", "cname", "wdi_unempilo", "wdi_povgap190", "mad_gdppc", "cai_request", "wdi_homicides")]
after that i had to create a new variable for each year, then use this to recode the original variable which was causing issues, and then delete the created variables:
d$abort_2016 <- ifelse(d$Country=="Uruguay" & d$Year==2016, 1, d$abort)
d$abort_2017 <- ifelse(d$Country=="Uruguay" & d$Year==2017, 1, d$abort)
d$abort_2018 <- ifelse(d$Country=="Uruguay" & d$Year==2018, 1, d$abort)
d$abort <- ifelse(d$abort_2016 == 1, 1, d$abort)
d$abort <- ifelse(d$abort_2017 == 1, 1, d$abort)
d$abort <- ifelse(d$abort_2018 == 1, 1, d$abort)
d <- subset(d, select = -c(abort_2016,abort_2016) )
d <- subset(d, select = -c(abort_2017,abort_2017) )
d <- subset(d, select = -c(abort_2018,abort_2018) )
This solved my issue
Upvotes: 0
Reputation: 472
Adding to the answer of rjen (while also using his/her dataframe df ) I would suggest you might use dplyr::case_when, such that
library(dplyr)
df %>% mutate(Abort =
case_when(drap %in% c(7.826785, 8.263874, 12.060453) ~ 1 ## if true
,TRUE ~ Abort) ## if false condition
)
Here, if drap takes a certain value (similary to your 2nd approach) Abort becomes 1. Otherwise Abort stays the same. It might be more useful than the regular ifelse condition, as you can connect even more conditions together. You could additionally add something like
case_when(drap %in% c(7.826785, 8.263874, 12.060453) & Year == 2013 ~ 1
Upvotes: 0
Reputation: 1972
The below is a tidyverse option based on the assumptions that drap
is of type double and that x.
is really NA
.
library(dplyr)
mutate(df, Abort = if_else(!is.na(drap), 1, Abort))
## A tibble: 7 x 4
# Year Country Abort drap
# <dbl> <chr> <dbl> <dbl>
# 1 2012 Uruguay 0 NA
# 2 2013 Uruguay 1 NA
# 3 2014 Uruguay 1 NA
# 4 2015 Uruguay 1 NA
# 5 2016 Uruguay 1 7.83
# 6 2017 Uruguay 1 8.26
# 7 2018 Uruguay 1 12.1
Data
df <- structure(list(Year = c(2012, 2013, 2014, 2015, 2016, 2017, 2018
), Country = c("Uruguay", "Uruguay", "Uruguay", "Uruguay", "Uruguay",
"Uruguay", "Uruguay"), Abort = c(0, 1, 1, 1, 0, 0, 0), drap = c(NA,
NA, NA, NA, 7.826785, 8.263874, 12.060453)), row.names = c(NA,
-7L), class = c("tbl_df", "tbl", "data.frame"))
Upvotes: 1