如何重新分类数据框列?
How to reclassify dataframe column?
我正在重新class验证一个数据框列中的值并将这些值添加到另一列。以下脚本尝试将重新class化函数应用于列并将值输出到数据框中的另一列。
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))
错误信息:
> 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
预期的输出应如下所示
a = c(1,2,3,4,5,6,7)
b = c(1,1,1,0,0,1,1)
x = data.frame(a, b)
我读过一个看似相似的问题 (Reclassify select columns in Data Table),尽管它似乎是在解决更改列的实际 class(例如数字)的问题。
如何从数据框中的一列中获取值,应用我的重新class化函数,并将值输出到新列?
在这里,我只是做(类似的事情):
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
你可以这样做:
library(dplyr)
mutate(x, b = ifelse(a %in% c(1, 2, 3, 6, 7), 1, 0))
我正在重新class验证一个数据框列中的值并将这些值添加到另一列。以下脚本尝试将重新class化函数应用于列并将值输出到数据框中的另一列。
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))
错误信息:
> 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
预期的输出应如下所示
a = c(1,2,3,4,5,6,7)
b = c(1,1,1,0,0,1,1)
x = data.frame(a, b)
我读过一个看似相似的问题 (Reclassify select columns in Data Table),尽管它似乎是在解决更改列的实际 class(例如数字)的问题。
如何从数据框中的一列中获取值,应用我的重新class化函数,并将值输出到新列?
在这里,我只是做(类似的事情):
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
你可以这样做:
library(dplyr)
mutate(x, b = ifelse(a %in% c(1, 2, 3, 6, 7), 1, 0))