I am working on reclassifying values in one dataframe column and adding those values to another column. The following script attempts to apply a reclassification function to a column and output the values to another column in a dataframe.
a = c(1,2,3,4,5,6,7)
x = data.frame(a)
# Reclassify values in x$a
reclass = function(x){
# 1 - Spruce/Fir = 1
# 2 - Lodgepole Pine = 1
# 3 - Ponderosa Pine = 1
# 4 - Cottonwood/Willow = 0
# 5 - Aspen = 0
# 6 - Douglas-fir = 1
# 7 - Krummholz = 1
if(x == 1) return(1)
if(x == 2) return(1)
if(x == 3) return(1)
if(x == 4) return(0)
if(x == 5) return(0)
if(x == 6) return(1)
if(x == 7) return(1)
}
# Add a new column
x$b = 0
# Apply function on new column
b = lapply(x$b, reclass(x$a))
The error message:
> b = lapply(x$b, reclass(x$a))
Error in match.fun(FUN) :
'reclass(x$a)' is not a function, character or symbol
In addition: Warning message:
In if (x == 1) return(1) :
the condition has length > 1 and only the first element will be used
The intended output should look like the following
a = c(1,2,3,4,5,6,7)
b = c(1,1,1,0,0,1,1)
x = data.frame(a, b)
I have read a seemingly similar question (Reclassify select columns in Data Table), although it appears to be addressing changing the actual class (e.g. numeric) of a column.
How can I take values from one column in a dataframe, apply my reclassification function, and output the values to a new column?
Here, I'd just do (something like):
coniferTypes <- c(1,2,3,6,7)
x$b <- as.integer(x$a %in% coniferTypes)
x
# a b
# 1 1 1
# 2 2 1
# 3 3 1
# 4 4 0
# 5 5 0
# 6 6 1
# 7 7 1
You could do:
library(dplyr)
mutate(x, b = ifelse(a %in% c(1, 2, 3, 6, 7), 1, 0))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With