我有一个可变的人员列表,作为数据框中的一长行,我有兴趣将这些记录重新组织成更有意义的格式。
我的原始数据看起来像这样,
df <- data.frame(name1 = "John Doe", email1 = "John@Doe.com", phone1 = "(444) 444-4444", name2 = "Jane Doe", email2 = "Jane@Doe.com", phone2 = "(444) 444-4445", name3 = "John Smith", email3 = "John@Smith.com", phone3 = "(444) 444-4446", name4 = NA, email4 = "Jane@Smith.com", phone4 = NA, name5 = NA, email5 = NA, phone5 = NA)
df
# name1 email1 phone1 name2 email2 phone2
# 1 John Doe John@Doe.com (444) 444-4444 Jane Doe Jane@Doe.com (444) 444-4445
# name3 email3 phone3 name4 email4 phone4 name5
# 1 John Smith John@Smith.com (444) 444-4446 NA Jane@Smith.com NA NA
# email5 phone5
# 1 NA NA
我正在尝试将其弯曲成这样的格式,
df_transform <- structure(list(name = structure(c(2L, 1L, 3L, NA, NA), .Label = c("Jane Doe",
"John Doe", "John Smith"), class = "factor"), email = structure(c(3L,
1L, 4L, 2L, NA), .Label = c("Jane@Doe.com", "Jane@Smith.com",
"John@Doe.com", "John@Smith.com"), class = "factor"), phone = structure(c(1L,
2L, 3L, NA, NA), .Label = c("(444) 444-4444", "(444) 444-4445",
"(444) 444-4446"), class = "factor")), .Names = c("name", "email",
"phone"), class = "data.frame", row.names = c(NA, -5L))
df_transform
# name email phone
# 1 John Doe John@Doe.com (444) 444-4444
# 2 Jane Doe Jane@Doe.com (444) 444-4445
# 3 John Smith John@Smith.com (444) 444-4446
# 4 <NA> Jane@Smith.com <NA>
# 5 <NA> <NA> <NA>
应该补充一点,它并不总是五个记录,它可以是 1 到 99 之间的任何数字。我尝试使用reshape2
'smelt
和 `t()1 但它变得复杂了。我想有一些我根本不知道的已知方法。