【问题标题】:count frequency of variable dependent on other variable in an R dataframe [duplicate]依赖于R数据帧中其他变量的变量计数频率[重复]
【发布时间】:2020-09-16 14:32:12
【问题描述】:
df <- data.frame(samples = c('45fe.K2','45fe.K2','45fe.K2','45hi.K1','45hi.K1'),source = c('f','f','o','o','f'))
df
  samples   sou
1 45fe.K2      f
2 45fe.K2      f
3 45fe.K2      o
4 45hi.K1      o
5 45hi.K1      f

我想计算samples中有多少来自soufo

结果应该是这样的

samples      sou count
1 45fe.K2      f 2
3 45fe.K2      o 1
4 45hi.K1      o 1
5 45hi.K1      f 1

我试过了

df <- df  %>%
  group_by(sou) %>%
  mutate(count = n_distinct(samples)) %>%
  ungroup()

df <- within(df, { count <- ave(sou, samples, FUN=function(x) length(unique(x)))})

df$count <- ave(as.integer(df$samples), df$sou, FUN = function(x) length(unique(x)))

df$count <- with(df, ave(samples,sou, FUN = function(x) length(unique(x))))

所有这些仅计算唯一的 samples(即 2)或唯一数量的 sou(即 2)。但我想知道独特样本中有多少独特的苏。

【问题讨论】:

    标签: r dataframe count word


    【解决方案1】:

    summarise()n() 试试这个dplyr 解决方案:

    library(dplyr)
    df %>% group_by(samples,source) %>% summarise(N=n())
    

    输出:

    # A tibble: 4 x 3
    # Groups:   samples [2]
      samples source     N
      <chr>   <chr>  <int>
    1 45fe.K2 f          2
    2 45fe.K2 o          1
    3 45hi.K1 k          1
    4 45hi.K1 o          1
    

    base R 解决方案将创建一个指标变量 N ,然后是 aggregate()

    #Data
    df$N <- 1
    #Code
    aggregate(N~samples+source,df,sum)
    

    输出:

      samples source N
    1 45fe.K2      f 2
    2 45hi.K1      k 1
    3 45fe.K2      o 1
    4 45hi.K1      o 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多