【问题标题】:Filling an empty matrix with values from tibble in R用 R 中 tibble 的值填充一个空矩阵
【发布时间】:2019-09-06 15:11:09
【问题描述】:

我生成了一个格式如下的小标题:

  V1 n
1 "Sam,Chris" 30
2 "Sam,Peter" 81
3 "Jeff,James" 5
4 "David,Jones" 6
5 "Harry,Otto" 8

我还有一个大矩阵,其中每一行和每一列都以一个名字命名,每个名字出现一次。所以我需要对V1的每一行进行拆分,这样矩阵的索引即为:

   [Sam]
[Chris]30

例如,所以我需要以某种方式用逗号分割然后填充矩阵,我该怎么做?

【问题讨论】:

    标签: r dataframe matrix


    【解决方案1】:

    我们可能需要使用separate_rows

    library(tidyverse)
    df1 %>%      
      separate_rows(V1, sep=",") 
    

    如果我们想得到matrix的输出

    df1 %>%
       separate(V1, into = c("V1", "V2"), sep=",") %>%
       spread(V2, n, fill = 0) %>%
       column_to_rownames("V1")
    #    Chris James Jones Otto Peter
    #David    0    0    6   0    0
    #Harry    0    0    0    8    0
    #Jeff     0     5   0   0  0
    #@Sam      30    0    0   0   81
    

    通过在行名和列名中包含名字和姓氏,可以将其转换为方阵

    tmp <- df1 %>%
           separate(V1, into = c("V1", "V2"), sep=",") 
    lvls <- sort(unique(unlist(tmp[1:2])))
    tmp %>% 
      mutate_at(vars(V1, V2), factor, levels = lvls) %>%
      spread(V2, n, fill = 0, drop = FALSE)
    

    数据

    df1 <- structure(list(V1 = c("Sam,Chris", "Sam,Peter", "Jeff,James", 
    "David,Jones", "Harry,Otto"), n = c(30L, 81L, 5L, 6L, 8L)), 
     class = "data.frame", row.names = c("1", 
    "2", "3", "4", "5"))
    

    【讨论】:

    • 是的,抱歉,另一列具有该值。我用我的集合尝试了这个,第一个 n 的输出是 NA,我能问我为什么要这样做吗?我真的不明白对不起
    • @RahimDina。在您的预期中,您显示了Sam 和 emtpy 值
    • @RahimDina。我更新了一个选项。请检查
    • @RahimDina。你用过tmp &lt;- df1 %&gt;% separate(V1, into = c("V1", "V2"), sep=","); lvls &lt;- sort(unique(unlist(tmp[1:2])))
    • 对不起,我一定错过了,谢谢大家的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多