是否有一种整洁的R方法可以将频率表转换为计数的数据帧?

问题描述 投票:0回答:1

我有一个频率表,但没有用于生成频率表的数据。我需要将其放入计数数据框。

# Create example
Species<- c("Speices1", "Speices2", "Speices1", "Speices3", "Speices3")
Annotation<- c("synthase", "transferase", "synthase", "RNAbinding", "synthase")
input_df<- data.frame(cbind(Species, Annotation))
input_df
  Species  Annotation
1 Speices1    synthase
2 Speices2 transferase
3 Speices1    synthase
4 Speices3  RNAbinding
5 Speices3    synthase

我的输入看起来像这样:

test_table<- table(input_df$Species, input_df$Annotation)
test_table 
     Var1       Var2        Freq
1   Speices1    RNAbinding  0
2   Speices2    RNAbinding  0
3   Speices3    RNAbinding  1
4   Speices1    synthase    2
5   Speices2    synthase    0
6   Speices3    synthase    1
7   Speices1    transferase 0
8   Speices2    transferase 1
9   Speices3    transferase 0

假设我无权访问用于生成频率表的输入数据帧,如何将频率表转换为以“ Species”作为列名,以“ Annotation”作为行名的数据框(是否有整齐的方法?):

          Species1 Species2 Species3
RNAbinding    0     0     1

synthase      2     0     1

transferase   0     1     0
r tidy
1个回答
0
投票

很确定这是重复的。寻找合适的骗子...

同时

library(dplyr)
library(tidyr)
input_df %>% 
    count(Species, Annotation) %>% 
    pivot_wider(names_from = "Species", values_from = "n")
#  Annotation  Speices1 Speices2 Speices3
#  <fct>          <int>    <int>    <int>
#1 synthase           2       NA        1
#2 transferase       NA        1       NA
#3 RNAbinding        NA       NA        1
© www.soinside.com 2019 - 2024. All rights reserved.