【发布时间】: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 方法。
【问题讨论】: