R-对应于复杂条件(包括字符串)的计数

R-对应于复杂条件(包括字符串)的计数,r,R,这是一个转载。 我有一个包含多行和多列的数据库。我找到了一种计算与自定义条件对应的行数的方法。 现在我对自定义字符串的计数有困难,例如所有包含“假单胞菌”的字符串。包括绿脓杆菌、假单胞菌等 示例(实际表格由5000行和200列组成): 我想知道这些数字: -里面有“假单胞菌”这个字符串吗 -第二栏是第四栏 -每人只计算一次 由于我需要在具有不同值的类似结构的数据集上多次运行脚本,所以我使用了一个自定义函数(“MACI”)。“x”是数据帧。“Var”是函数返回的变量。我使用summary和n_di

这是一个转载。 我有一个包含多行和多列的数据库。我找到了一种计算与自定义条件对应的行数的方法。 现在我对自定义字符串的计数有困难,例如所有包含“假单胞菌”的字符串。包括绿脓杆菌、假单胞菌等

示例(实际表格由5000行和200列组成):

我想知道这些数字: -里面有“假单胞菌”这个字符串吗 -第二栏是第四栏 -每人只计算一次

由于我需要在具有不同值的类似结构的数据集上多次运行脚本,所以我使用了一个自定义函数(“MACI”)。“x”是数据帧。“Var”是函数返回的变量。我使用summary和n_distinct来计算每个名称中只有一行。调用函数的“c2”变量用于自定义我需要第2列中的哪些数据

到目前为止,这是有效的,但在字符串中查找字符串却没有成功

MACI <- function(x, c2) {
  var <- 0
  for (i in 1:nrow(c2)) {
    var <- var + summarise(x, count = n_distinct(Name[
      MACI == 0 & (
        x$column2 == c2[i,1]
      )
      ]))
  }
#return
  return(var)
}


MACI这可能不是最优雅的答案,但我认为它是有效的

创建示例数据框:

df <- data.frame(name= c('John','John','Jack','Mary','Mary','Mary','Louise'),
count=c(4,4,5,4,5,4,3), 
bacteria=c('Staphylococcus', 'Pseudomonas aeruginosa' , 'Pseudomonas spp.', 'Klebsiella', 'Pseudomonas kompl.', 'Escherichia coli', 'Pseudomonas constell'), 
var=c('T','T','F','T','F','F','T'))
df数据:

df <- read.table(text = "John - 4  - Staphylococcus - T
                         John - 4  - Pseudomonas aeruginosa -T
                         Jack - 5  - Pseudomonas spp. - T
                         Mary - 4  - Klebsiella - F
                         Mary - 5  - Pseudomonas kompl. - T
                         Mary - 4  - Escherichia Coli - F
                         Louise- 3 - Pseudomonas constell", 
                 sep = "-",fill=TRUE)

如果我了解您要查找的内容,您将尝试统计满足3个条件的行数:

  • 第2列中的值等于4

  • 第3列中的字符串包含匹配的变量

  • 仅计算第1列中的唯一值

  • #使用示例数据
    图书馆(readr)
    图书馆(stringr)
    
    dt您在R中查找过可复制的示例吗?哈德利·威克姆有一个很好的解释,可以帮助你理解人们的要求。
    df <- data.frame(name= c('John','John','Jack','Mary','Mary','Mary','Louise'),
    count=c(4,4,5,4,5,4,3), 
    bacteria=c('Staphylococcus', 'Pseudomonas aeruginosa' , 'Pseudomonas spp.', 'Klebsiella', 'Pseudomonas kompl.', 'Escherichia coli', 'Pseudomonas constell'), 
    var=c('T','T','F','T','F','F','T'))
    
    results <- df[ grep ('Pseudomonas', df$bacteria, ignore.case=T),'name']
    
    NROW(results)
    
    df <- read.table(text = "John - 4  - Staphylococcus - T
                             John - 4  - Pseudomonas aeruginosa -T
                             Jack - 5  - Pseudomonas spp. - T
                             Mary - 4  - Klebsiella - F
                             Mary - 5  - Pseudomonas kompl. - T
                             Mary - 4  - Escherichia Coli - F
                             Louise- 3 - Pseudomonas constell", 
                     sep = "-",fill=TRUE)
    
    aggregate(V3~V1,df,function(x)sum(grepl("Pseudomonas",x)))
    
         V1 V3
    1  Jack   1
    2  John   1
    3 Louise  1
    4  Mary   1
    
    # Using your example data
    library(readr)
    library(stringr)
    
    dt <- read_delim (
          "John - 4  - Staphylococcus - T
          John - 4  - Pseudomonas aeruginosa -T
          Jack - 5  - Pseudomonas spp. - T
          Mary - 4  - Klebsiella - F
          Mary - 5  - Pseudomonas kompl. - T
          Mary - 4  - Escherichia Coli - F
          Louise- 3 - Pseudomonas constell",
          delim = "-", 
          col_names = F
    )
    dt$X2 <- as.integer(dt$X2)
    dt
    # # A tibble: 7 x 4
    #  X1                X2 X3                         X4   
    #  <chr>          <int> <chr>                      <chr>
    # 1 "John "            4 " Staphylococcus "         " T" 
    # 2 "      John "      4 " Pseudomonas aeruginosa " T    
    # 3 "      Jack "      5 " Pseudomonas spp. "       " T" 
    # 4 "      Mary "      4 " Klebsiella "             " F" 
    # 5 "      Mary "      5 " Pseudomonas kompl. "     " T" 
    # 6 "      Mary "      4 " Escherichia Coli "       " F" 
    # 7 "      Louise"     3 " Pseudomonas constell"    <NA> 
    
    detect <- function(x, mit) {
    ### following your example
         y <- x[str_detect(x$X3, mit),] %>%
                filter(X2 == 4)
         n_distinct(y$X1)
    }