【问题标题】:R Creating a data frame from a picture - is there a better way than what I'm doing here?R 从图片创建数据框 - 有没有比我在这里做的更好的方法?
【发布时间】:2020-11-16 03:26:51
【问题描述】:

我这里有这张照片。这是一个看起来像是在 Excel 中创建的表格。我正在尝试在 R 中创建一个数据框,只是复制这张图片。

我的第一次尝试是创建这些向量,然后使用 rownames() 函数添加行名。但是我发现行名并不是很有用,当我尝试使用 tidy::gather 方法时,它们就消失了。

English_E <- c(10, 1, 3, 2, 51)
Currier_C <- c(15, 2, 1, 4, 102)
Primrose_P1 <- c(10, 2, 6, 2, 66)
Primrose_P2 <- c(10, 1, 6, 5, 66)
Bluetail_B <- c(20, 1, 3, 3, 89)
Resource_Availability <- c(130, 13, 45, 23, "")

rownames(pottery_df) <- c("Clay_lbs", "Enamel_lbs", "Dry_Room_hrs", "Kiln_hrs", "Contribution_to_Earning")

所以我的第二种方法是创建这些相同的向量,然后创建另一个我称之为“注意事项”的向量,因为我没有更好的主意,然后我将它们全部转换为数据框,然后我执行 cbind 并删除列并重新排列它。我认为第二种方式要好得多,因为 rowname 列很有用。

English_E <- c(10, 1, 3, 2, 51)
Currier_C <- c(15, 2, 1, 4, 102)
Primrose_P1 <- c(10, 2, 6, 2, 66)
Primrose_P2 <- c(10, 1, 6, 5, 66)
Bluetail_B <- c(20, 1, 3, 3, 89)
Resource_Availability <- c(130, 13, 45, 23, "")

Considerations <-  c("Clay_lbs", "Enamel_lbs", "Dry_Room_hrs", "Kiln_hrs", "Contribution_to_Earning")

English_E_df <- data_frame(English_E) %>% cbind(Considerations)
Currier_C_df <- data_frame(Currier_C) %>% cbind(Considerations)
Primrose_P1_df <- data_frame(Primrose_P1) %>% cbind(Considerations)
Primrose_P2_df <- data_frame(Primrose_P2) %>% cbind(Considerations)
Bluetail_B_df <- data_frame(Bluetail_B) %>% cbind(Considerations)
Resource_Availability_df <- data_frame(Resource_Availability) %>% cbind(Considerations)

pottery_df <- cbind(English_E_df, Currier_C_df, Primrose_P1_df, Primrose_P2_df, Bluetail_B_df, Resource_Availability_df) 

pottery_df <- pottery_df %>%
  select(1, 2, 3, 5, 7, 9, 11) 

pottery_df <- pottery_df[, c(2, 1, 3, 4, 5, 6, 7)]

我的问题——有没有更好的方法来做这一切?我觉得这是创建一个非常简单的表的大量代码,并且创建这么多表并将它们组合在一起并删除重复列似乎是一种 hackjob 方法。

【问题讨论】:

    标签: r dataframe tidyverse


    【解决方案1】:

    tibbles 不支持行名,所以将行名的信息作为单独的列。

    English_E <- c(10, 1, 3, 2, 51)
    Currier_C <- c(15, 2, 1, 4, 102)
    Primrose_P1 <- c(10, 2, 6, 2, 66)
    Primrose_P2 <- c(10, 1, 6, 5, 66)
    Bluetail_B <- c(20, 1, 3, 3, 89)
    Resource_Availability <- c(130, 13, 45, 23, "")
    name <- c("Clay_lbs", "Enamel_lbs", "Dry_Room_hrs", "Kiln_hrs", "Contribution_to_Earning")
    pottery_df <- data.frame(name, English_E, Currier_C, Primrose_P1, Primrose_P2, Bluetail_B, Resource_Availability)
    

    【讨论】:

      【解决方案2】:

      一种方法是使用tribble() function

      library(tidyverse)
      dataframe <- tribble(~"Rownames", ~"English_E", ~"Currier_C", ~"Primrose_P1", ~"Primrose_P2", ~"Bluetail_B", ~"Resource_Availability",
                           "Clay_lbs", 10, 15, 10, 10, 20, 130,
                           "Enamel_lbs", 1, 2, 2, 1, 1, 13,
                           "Dry_Room_hrs", 3, 1, 6, 6, 3, 13,
                           "Kiln_hrs", 2, 4, 2, 5, 3, 45,
                           "Contribution_to_Earning", 51, 102, 66, 66, 89, 23)
      
      dataframe
      # A tibble: 5 x 7
        Rownames English_E Currier_C Primrose_P1 Primrose_P2 Bluetail_B
        <chr>        <dbl>     <dbl>       <dbl>       <dbl>      <dbl>
      1 Clay_lbs        10        15          10          10         20
      2 Enamel_…         1         2           2           1          1
      3 Dry_Roo…         3         1           6           6          3
      4 Kiln_hrs         2         4           2           5          3
      5 Contrib…        51       102          66          66         89
      # … with 1 more variable: Resource_Availability <dbl>
      

      您可以使用column_to_rownames() 将Rownames 变量用作行名:

      library(tidyverse)
      dataframe <- tribble(~"Rownames", ~"English_E", ~"Currier_C", ~"Primrose_P1", ~"Primrose_P2", ~"Bluetail_B", ~"Resource_Availability",
                           "Clay_lbs", 10, 15, 10, 10, 20, 130,
                           "Enamel_lbs", 1, 2, 2, 1, 1, 13,
                           "Dry_Room_hrs", 3, 1, 6, 6, 3, 13,
                           "Kiln_hrs", 2, 4, 2, 5, 3, 45,
                           "Contribution_to_Earning", 51, 102, 66, 66, 89, 23) %>% 
        column_to_rownames(var = "Rownames")
      
      dataframe
                              English_E Currier_C Primrose_P1 Primrose_P2 Bluetail_B Resource_Availability
      Clay_lbs                       10        15          10          10         20                   130
      Enamel_lbs                      1         2           2           1          1                    13
      Dry_Room_hrs                    3         1           6           6          3                    13
      Kiln_hrs                        2         4           2           5          3                    45
      Contribution_to_Earning        51       102          66          66         89                    23
      

      【讨论】:

        猜你喜欢
        • 2012-05-08
        • 1970-01-01
        • 2011-03-09
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多