【发布时间】:2020-09-02 23:43:48
【问题描述】:
我正在尝试将数据帧从长格式转换为宽格式,但由于其结构,每当我使用 pivot_wider() 时,我都会得到两列包含数据向量的列。
这是原始数据:
structure(list(type = c("radio", "radio", "radio", "television",
"television", "television", "television", "television", "television",
"television", "television", "television", "television", "television",
"television", "television", "television", "television", "television"
), Resource = c("samsung", "samsung", "samsung", "samsung", "samsung",
"samsung", "samsung", "samsung", "samsung", "samsung", "samsung",
"sony", "sony", "sony", "sony", "sony", "sony", "sony", "sony"
), Property = c("lot_number", "lot_number", "manufacturer", "lot_number",
"lot_number", "lot_number", "lot_number", "lot_number", "manufacturer",
"other_PN", "part_number", "lot_number", "lot_number", "lot_number",
"lot_number", "lot_number", "manufacturer", "other_PN", "part_number"
), value = c("12345", "54321", "John", "9876", "12345", "54321",
"56789", "67890", "Walt", "5g6h3f", "6789", "9876", "12345",
"54321", "56789", "67890", "John", "2a3b4c", "3461")), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -19L), spec = structure(list(
cols = list(type = structure(list(), class = c("collector_character",
"collector")), Resource = structure(list(), class = c("collector_character",
"collector")), Property = structure(list(), class = c("collector_character",
"collector")), value = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
我使用的命令:
df_wide <- df %>% pivot_wider(names_from = Property, values_from = value)
但多个值被保存为“lot_number”列中的向量:
structure(list(type = c("radio", "television", "television"),
Resource = c("samsung", "samsung", "sony"), lot_number = list(
c("12345", "54321"), c("9876", "12345", "54321", "56789",
"67890"), c("9876", "12345", "54321", "56789", "67890"
)), manufacturer = list("John", "Walt", "John"), other_PN = list(
NULL, "5g6h3f", "2a3b4c"), part_number = list(NULL, "6789",
"3461")), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
这是我最后想要得到的数据框。请注意,在所需的输出中,“radio”有一些缺失值。此外,“manufacturer”、“other_PN”和“part_number”列中的值必须在多行中重复。
structure(list(type = c("radio", "radio", "television", "television",
"television", "television", "television", "television", "television",
"television", "television", "television"), Resource = c("samsung",
"samsung", "samsung", "samsung", "samsung", "samsung", "samsung",
"sony", "sony", "sony", "sony", "sony"), manufacturer = c("John",
"John", "Walt", "Walt", "Walt", "Walt", "Walt", "John", "John",
"John", "John", "John"), other_PN = c(NA, NA, "5g6h3f", "5g6h3f",
"5g6h3f", "5g6h3f", "5g6h3f", "2a3b4c", "2a3b4c", "2a3b4c", "2a3b4c",
"2a3b4c"), part_number = c(NA, NA, 6789, 6789, 6789, 6789, 6789,
3461, 3461, 3461, 3461, 3461), lot_number = c(12345, 54321, 9876,
12345, 54321, 56789, 67890, 9876, 12345, 54321, 56789, 67890)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), spec = structure(list(
cols = list(type = structure(list(), class = c("collector_character",
"collector")), Resource = structure(list(), class = c("collector_character",
"collector")), manufacturer = structure(list(), class = c("collector_character",
"collector")), other_PN = structure(list(), class = c("collector_character",
"collector")), part_number = structure(list(), class = c("collector_double",
"collector")), lot_number = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
感谢您的帮助!!
【问题讨论】: