【问题标题】:How to reclassify dataframe column?如何重新分类数据框列?
【发布时间】:2015-07-03 13:45:35
【问题描述】:

我正在重新分类一个数据框列中的值并将这些值添加到另一列。以下脚本尝试将重分类函数应用于列并将值输出到数据框中的另一列。

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),尽管它似乎是在解决更改列的实际类(例如数字)的问题。

如何从数据框中的一列中获取值,应用我的重分类函数,并将值输出到新列?

【问题讨论】:

    标签: r dataframe multiple-columns


    【解决方案1】:

    在这里,我会做(类似的事情):

    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
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      library(dplyr)
      mutate(x, b = ifelse(a %in% c(1, 2, 3, 6, 7), 1, 0))
      

      【讨论】:

        猜你喜欢
        • 2019-08-30
        • 2020-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多