Home > database >  Replace 0 values by NA for selected columns based on condition in R
Replace 0 values by NA for selected columns based on condition in R

Time:07-19

I would like to replace the 0 values within cols 10:37 by NA when year = 0.

I have tried to following codes:

ACLED_PRIO_flanked[which(ACLED_PRIO_flanked$year == "0")] <- NA

ACLED_PRIO_flanked[c(10:37),][ACLED_PRIO_flanked$year == 0] <- NA

is.na(ACLED_PRIO_flanked[,10:37]) <- ACLED_PRIO_flanked$year== 0

but I get the following message:

Error in vectbl_as_col_location(): ! Must assign to columns with a valid subscript vector. ℹ Logical subscripts must match the size of the indexed input. ✖ Input has size 28 but subscript value has size 17494.

or

Error in numtbl_as_col_location_assign(): ! Can't assign to columns beyond the end with non-consecutive locations. ℹ Input has size 37. ✖ Subscript which(ACLED_PRIO_flanked$year == "0") contains non-consecutive locations 52, 96, 103, 137, 157, etc.

the dataset:

structure(list(gid = c("100467", "100468", "100469", "100470", 
"100471", "100472", "100473", "100474", "100475", "100476", "100477", 
"100478", "100479", "100480", "100481", "100482", "100483", "100484", 
"100485", "100486", "100487", "100488", "100489", "100490", "100491", 
"100492", "100493", "100494", "100495", "100496", "100496", "100497", 
"100497", "100497", "100497", "100497", "100497", "100497", "100498", 
"100498"), xcoord = c(13.25, 13.75, 14.25, 14.75, 15.25, 15.75, 
16.25, 16.75, 17.25, 17.75, 18.25, 18.75, 19.25, 19.75, 20.25, 
20.75, 21.25, 21.75, 22.25, 22.75, 23.25, 23.75, 24.25, 24.75, 
25.25, 25.75, 26.25, 26.75, 27.25, 27.75, 27.75, 28.25, 28.25, 
28.25, 28.25, 28.25, 28.25, 28.25, 28.75, 28.75), ycoord = c(-20.25, 
-20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25, 
-20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25, 
-20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25, 
-20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25, 
-20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25), col = c("387", 
"388", "389", "390", "391", "392", "393", "394", "395", "396", 
"397", "398", "399", "400", "401", "402", "403", "404", "405", 
"406", "407", "408", "409", "410", "411", "412", "413", "414", 
"415", "416", "416", "417", "417", "417", "417", "417", "417", 
"417", "418", "418"), row = c("140", "140", "140", "140", "140", 
"140", "140", "140", "140", "140", "140", "140", "140", "140", 
"140", "140", "140", "140", "140", "140", "140", "140", "140", 
"140", "140", "140", "140", "140", "140", "140", "140", "140", 
"140", "140", "140", "140", "140", "140", "140", "140"), gwno = c(565, 
565, 565, 565, 565, 565, 565, 565, 565, 565, 565, 565, 565, 565, 
565, 565, 571, 571, 571, 571, 571, 571, 571, 571, 571, 571, 571, 
571, 571, 552, 552, 552, 552, 552, 552, 552, 552, 552, 552, 552
), country = c("Namibia", "Namibia", "Namibia", "Namibia", "Namibia", 
"Namibia", "Namibia", "Namibia", "Namibia", "Namibia", "Namibia", 
"Namibia", "Namibia", "Namibia", "Namibia", "Namibia", "Botswana", 
"Botswana", "Botswana", "Botswana", "Botswana", "Botswana", "Botswana", 
"Botswana", "Botswana", "Botswana", "Botswana", "Botswana", "Botswana", 
"Zimbabwe (Rhodesia)", "Zimbabwe (Rhodesia)", "Zimbabwe (Rhodesia)", 
"Zimbabwe (Rhodesia)", "Zimbabwe (Rhodesia)", "Zimbabwe (Rhodesia)", 
"Zimbabwe (Rhodesia)", "Zimbabwe (Rhodesia)", "Zimbabwe (Rhodesia)", 
"Zimbabwe (Rhodesia)", "Zimbabwe (Rhodesia)"), km2 = c(3282.64642089, 
3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 
3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 
3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 
3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 
3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 
3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 
3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089, 
3282.64642089, 3282.64642089, 3282.64642089, 3282.64642089), 
    year = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2000, 2002, 2001, 
    2007, 2002, 2004, 2008, 2003, 2000, 2002, 2010), `Violence against civilians` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 1, 1, 1, 1, 2, 1, 1, 26, 
    6), Protests = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 
    0, 0, 0, 0, 0, 0, 6), `Strategic developments` = c(0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0), 
    Riots = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), Battles = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), `Explosions/Remote violence` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0), TotalConflicts = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 
    1, 3, 1, 1, 2, 1, 1, 29, 13), IncidenceConflicts = c(0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
    LagIncidenceConflicrs = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    1, 0, 1, 1, 1, 1, 1, 1, 0, 1), OnsetConflicts = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0), IncidenceBattles = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    1), LagIncidenceBattles = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), OnsetBattles = c(0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), 
    IncidenceERV = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0), LagIncidenceERV = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), OnsetERV = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0), IncidenceRiots = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), LagIncidenceRiots = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), OnsetRiots = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0), IncidenceSD = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 1, 0), LagIncidenceSD = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), OnsetSD = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
    0), IncidenceVAC = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1), LagIncidenceVAC = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1), OnsetVAC = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 
    0), IncidenceProtests = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 0, 0, 1), LagIncidenceProtests = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
    0), OnsetProtests = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 1, 0, 0, 0, 0, 0, 0, 1)), row.names = c(NA, -40L), class = c("tbl_df", 
"tbl", "data.frame"))

CodePudding user response:

We can use across to loop over the column and replace the values where the value is 0 and the 'year' column value is 0

library(dplyr)
df1 <- df1 %>%
   mutate(across(10:37,  ~replace(.x, .x == 0 & year == 0, NA)))

CodePudding user response:

Try this

df[10:37] <- lapply(df[10:37] , \(x) x <- ifelse(x == 0 & df$year == 0 , NA , x))

CodePudding user response:

OK, found this working as well:

DF[c(10:37)][which(DF$year >= 0), ] <- NA

  • Related