【问题标题】:how to get total number of a variable in each row如何获取每行中变量的总数
【发布时间】:2018-11-11 04:20:12
【问题描述】:

我有一个如下名称 sp 的数据框

Join      p1     sp1       p2      sp2     p3      sp3
  1          0        0           0         0        0          0
   2          1        pine     0         0       1         Aspen
   3           2        pine     0        0       0          0

数据框以 100 行继续,其中 p1 是 sp1 列指示的物种数,依此类推。 现在我想创建一个新的变量 pine 来计算树种的总数 每行松(加入)

【问题讨论】:

    标签: r


    【解决方案1】:

    一个简单的apply 按行就可以了。我对 data.frame 进行子集化,使用grep 获取以"sp" 开头的列。

    pine <- apply(sp[grep("^sp", names(sp))], 1, function(x) sum(x == "pine"))
    pine
    #[1] 0 1 1
    

    数据。

    sp <- 
    structure(list(Join = 1:3, p1 = 0:2, sp1 = structure(c(1L, 2L, 
    2L), .Label = c("0", "pine"), class = "factor"), p2 = c(0L, 0L, 
    0L), sp2 = c(0L, 0L, 0L), p3 = c(0L, 1L, 0L), sp3 = structure(c(1L, 
    2L, 1L), .Label = c("0", "Aspen"), class = "factor")), class = "data.frame", row.names = c(NA, 
    -3L))
    

    【讨论】:

    • 我使用了这个参数,我只得到了整个数据集的 0,1 值。​​
    • @MostarinBinteAhmed 我看不出上面的代码有什么问题,在您发布的数据集中,这些结果是正确的,每行有 0 或 1 列 sp 等于 "pine"
    • 在真实数据集中,我有 1000 行,某些行中的松树数超过 0,1。当我为整个数据集应用代码时,我只得到所有行的 01,这在现实中并不相同
    【解决方案2】:

    您可以将数据转换为长格式以执行计算。一旦数据为长格式,fuzzyjoin::regex_inner_join 将允许为配对值加入数据(例如p1 vs sp1)。

    使用tidyverse 的选项可以是:

    library(tidyverse)
    library(fuzzyjoin)         
    
    #To calculate count of Species per row for different type
    
    df %>% gather(Species, value, -Join) %>% 
      mutate(Join = as.character(Join))  %>% {
        regex_inner_join(filter(., grepl("^s",Species)),
                  filter(.,grepl("^p",Species)),
                  by = c("Join", "Species"))
    } %>%
      filter(value.x != "0") %>%
      group_by(Join.x, value.x) %>%
      summarise(count = sum(as.numeric(value.y))) %>% as.data.frame()
    
    #   Join.x value.x count
    # 1      2   Aspen     1
    # 2      2    pine     1
    # 3      3    pine     2
    
    #To calculate count of Species per row 
    df %>% gather(Species, value, -Join) %>% 
      mutate(Join = as.character(Join))  %>% {
        regex_inner_join(filter(., grepl("^s",Species)),
                  filter(.,grepl("^p",Species)),
                  by = c("Join", "Species"))
    } %>%
    group_by(Join.x) %>%
    summarise(count = sum(as.numeric(value.y))) %>% as.data.frame()
    
    #   Join.x count
    # 1      1     0
    # 2      2     2
    # 3      3     2
    

    数据:

    df <- read.table(text = 
    "Join      p1     sp1       p2      sp2     p3      sp3
    1          0        0           0         0        0          0
    2          1        pine     0         0       1         Aspen
    3           2        pine     0        0       0          0",
    header = TRUE, stringsAsFactors = FALSE)
    

    【讨论】:

    • 可能是我无法弄清楚问题。 p1 是 sp1 的出现次数,同样适用于 p2、sp2 等。我想知道每行中有多少松树(加入)。就像在第 2 行一样,有 1 棵松树,我怎么知道每一行的这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2012-08-06
    • 1970-01-01
    • 2020-03-05
    相关资源
    最近更新 更多