Hi,
I have this simple data frame:
data.frame(stringsAsFactors=FALSE,
URN = c("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii"),
E1 = c(1, 2, 3, NA, NA, NA, NA, NA, NA),
string = c("ddd", "s", NA, "fr", "rf", "f", "aa", "v", "bg"),
D1 = c(-1, 10, 6, -1, 8, 9, 7, -1, 99)
)
and I would like to create new variable indicating NAs in the "string" variable.
data.frame<- data.frame%>%
mutate(XXX = if_else(is.na(x = data.frame$string),1,0))
I used this code end everything works fine (XXX for URN=ccc is 1)
Now, I have another data frame:
data.frame(
VitalCode = c(223L, 221L, 221L, 221L, 2415L),
InvoiceTotal = c(11.57, 35.33, 33.06, 25.04, 276.15),
LabourTotal = c(65, 130, 130, 137, 215.58),
PartsTotal = c(31.31, 191.11, 197.55, 73.49, 60.57),
AuraURNMatch = c(0, 0, 0, 0, 0),
DealerName = as.factor(c("AAA", "BBB", "CCC", "DDD", "EEE")),
Accountnum = as.factor(c("A", "A", "A", "A", "B")),
Division = as.factor(c("4W", "4W", "4W", "4W", "4W")),
ReferenceNumber = as.factor(c("ad", "das", "ee", "ad", "ad")),
VITAL.URN = as.factor(c(NA, NA, NA, NA, "V20619738065")),
URN = as.factor(c(NA, NA, NA, NA, "VE21511906256751")),
RoDate = as.factor(c("17/12/2012 00:00", "05/03/2013 00:00",
"15/03/2013 00:00",
"10/06/2014 00:00",
"26/06/2019 00:00")),
OperationCode = as.factor(c("MEC", "MEC", "MEC", "MST", "Service|Other")),
WorkDescription = as.factor(c("aaa", "sadd", "sfe", "gdtt", "gret")),
JobType = as.factor(c("Retail", "Retail", "Retail", "Retail",
"Retail")),
ServiceFlag = as.factor(c("NO", "NO", "NO", "YES", "YES")),
MOTFlag = as.factor(c("NO", "NO", "NO", "YES", "NO"))
)
but the same code does not generate what I need:
data.frame <- data.frame %>%
mutate(XXX= if_else(is.na(x = data.frame$aura.URN),1,0))
because I have 0s for all URNs (the last one should generate 1).
Any ideas?