【问题标题】:Change values of columns on conditional bases (Missing Data and Categorical)根据条件更改列的值(缺失数据和分类)
【发布时间】:2021-04-19 16:33:24
【问题描述】:

对于一份问卷,我想制作一份 df1 的副本,其中 x 列发生以下两件事:

  1. 将 x 的缺失数据(编码为 0)替换为“-1”。所以所有的 0 都必须变成 -1。

  2. x 的特定部分以数字而非类别编码。我创建了一个函数来将不同的类别分配给不同的值

categorise <- function(a_vector) { a_vector = case_when(
        a_vector >= 0 & a_vector < 50 ~ 1,
        a_vector >= 50 & a_vector < 500 ~ 2,
        a_vector >= 500 & a_vector < 5000 ~ 3,
        a_vector >= 5000 & a_vector < 50000 ~ 4,
        a_vector >= 50000 & a_vector < 500000 ~ 5,
        a_vector >= 500000 & a_vector < 5000000 ~ 6,
        a_vector >= 5000000 & a_vector < 50000000 ~ 7,
        a_vector >= 50000000 & a_vector < 500000000 ~ 8)
        strong texta_vector }

【问题讨论】:

    标签: r vector transform apply na


    【解决方案1】:

    我认为你可以这样写categorise_losses函数:

    categorise_losses <- function(x)
      as.integer(log10(x / 5)) + 1L
    
    # an example of using the function
    categorise_losses(c(1L, 10L, 65L, 250L, 555L, 5000L))
    #R> [1] 1 1 2 2 3 4
    
    # compare with the OP's function
    library(dplyr)
    categorise_losses_OP <- function(x)
      case_when(
        x >= 0 & x < 50 ~ 1,
        x >= 50 & x < 500 ~ 2,
        x >= 500 & x < 5000 ~ 3,
        x >= 5000 & x < 50000 ~ 4,
        x >= 50000 & x < 500000 ~ 5,
        x >= 500000 & x < 5000000 ~ 6,
        x >= 5000000 & x < 50000000 ~ 7,
        x >= 50000000 & x < 500000000 ~ 8)
    
    # we get the same
    all.equal(categorise_losses_OP(1:500000), 
              categorise_losses   (1:500000))
    #R> [1] TRUE
    

    要处理0变成-9的情况,可以使用:

    categorise_losses <- function(x)
      suppressWarnings(ifelse(x == 0, -9L, as.integer(log10(x / 5)) + 1L))
    
    categorise_losses(c(0L, 1L, 10L, 65L, 250L, 555L, 5000L))
    #R> [1] -9  1  1  2  2  3  4
    

    要对列条目的子集使用该函数,您可以使用$ 访问该列,然后使用[] 将您需要的条目子集,如下所示:

    # data set example
    dat <- data.frame(year = c(1950L, 1950L, 1950L, 2010L, 2010L, 2010L), 
                      crop_loss = c(0L, 5L, 95L, -9L, -9L, 1L))
    
    # use the function on the data
    categorise_losses <- function(x)
      suppressWarnings(ifelse(x == 0, -9L, as.integer(log10(x / 5)) + 1L))
    
    dat$crop_loss[dat$year <= 2006L] <- 
      categorise_losses(dat$crop_loss[dat$year <= 2006L])
    
    # the result
    dat
    #R>   year crop_loss
    #R> 1 1950        -9
    #R> 2 1950         1
    #R> 3 1950         2
    #R> 4 2010        -9
    #R> 5 2010        -9
    #R> 6 2010         1
    

    【讨论】:

    • 非常感谢您的全面回答!我是 R 的初学者,所以我不确定如何应用上面的代码。我在我的问题中添加了数据集的一部分和一些条件。你能帮我写出正确的代码吗?感谢转发!
    【解决方案2】:

    我们可以使用findInterval

    replace(findInterval(a_vector, c(0, 50, 500, 5000, 50000, 
          500000, 5000000, 50000000)), a_vector == 0, -9)
    #[1] -9  1  1  2  2  3  4
    

    cut

    as.integer(cut(a_vector, breaks =  c(0, 50, 500, 5000, 50000, 500000, 5000000, 50000000)))
    

    数据

    a_vector <- c(0L, 1L, 10L, 65L, 250L, 555L, 5000L)
    

    【讨论】:

    • 谢谢!并用'-9'改变'0'?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2018-05-14
    • 2023-02-21
    • 2022-01-25
    • 2022-11-30
    • 2020-12-01
    • 1970-01-01
    相关资源
    最近更新 更多