I have a dataframe of hail events and their numeric related damages. I want to add a column (DAMAGE_PROP) that has factors of “yes” or “no” to indicate whether or not there was damage to the property as indicated in the existing column DAMAGE_PROPERTY_NUM. With the code I’m currently using, I’m getting an “unused argument” error (levels = 2, labels = c(“yes”, “no”)).
Can you assist? Here’s the code snippet, and the table of listed damages:
AZ_HAIL$DAMAGE_PROP <- as.factor(AZ_HAIL$DAMAGE_PROPERTY_NUM,
levels = 2,
labels = c("yes", "no"))
> table(AZ_HAIL$DAMAGE_PROPERTY_NUM)
$0 $250 $400 $500 $1000 $2000 $5000 $6000 $10000 $15000 $20000 etc.
1163 2 1 5 5 4 12 1 7 1 3
I also tried the more general code
factor(x = character(), levels, labels = levels,
exclude = NA, ordered = is.ordered(x), nmax = NA)
not using "as.factor"
> AZ_HAIL$DAMAGE_PROP <- factor(AZ_HAIL$DAMAGE_PROPERTY_NUM = character(),
Error: unexpected '=' in "AZ_HAIL$DAMAGE_PROP <- factor(AZ_HAIL$DAMAGE_PROPERTY_NUM ="
I have also tried
answer <- factor(c("yes", "no"))
type <- unlist(lapply(answer, function(x) ifelse(as.numeric(substr(as.character(x), 2, nchar(as.character(x))))>0, 'yes', 'no')))
damage <- data.frame(answer=answer, type=factor(type))
is.factor(AZ_HAIL$DAMAGE_PROP)
damage
results:
answer type
1 yes <NA>
2 no <NA>
Bacon James is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.