【发布时间】:2021-11-13 14:39:06
【问题描述】:
我想重塑一个文件,但重塑的典型用法不适用于我的问题(我不认为)。我可以手动完成,但很容易出错,而且我不会学习如何使用 R 来完成这项工作。所以在阅读了 Reshape 和 Tidyr 之后,我决定来这里。
我有一份电力公司及其运营所在县的档案。我希望能够重塑它,使其成为一个县列表以及其中的电力设施。我还有一个变量,用于判断我是否在另一个名为“InData”的数据集中为每个实用程序提供数据。然后我会在此基础上确定每个县的缺失程度。
列名是:
UtilityName, InData, County1,County2,County3,County4... County12
有没有一种简单的方法来重塑它,还是我需要创建某种循环遍历每一列?
以下是数据示例:>
dput(utility_county)
structure(list(UtilityName = c("Alder Mutual Light Co Inc", "Avista Corporation",
"Asotin County PUD", "Benton County PUD", "Blaine City Light",
"Centralia City Light", "Chelan County PUD", "City of Cashmere",
"City of Chewelah, Electric Department", "City of Cheney", "City of Coulee Dam Light Dept",
"City of Ellensburg", "City of McCleary", "City of Milton", "City of Richland",
"City of Sumas", "Clallam County PUD", "Clark County PUD", "Clearwater Power",
"Columbia Rural Electric", "Cowlitz County PUD", "Douglas County PUD",
"Elmhurst Power & Light Co", "Ferry County PUD", "Franklin County PUD",
"Grant County PUD", "Grays Harbor County PUD", "Inland Power & Light",
"Jefferson County PUD", "Kittitas County PUD", "Klickitat County PUD",
"Kootenai Electric Cooperative Inc", "Lakeview Light & Power",
"Lewis County PUD", "Mason County PUD1", "Mason County PUD3",
"Modern Electric Water Company", "Nespelem Valley Electric Cooperative",
"Ohop Mutual Light Co", "Okanogan PUD", "Orcas Power and Light Coop",
"Pacific County PUD", "Pacific Power", "Parkland Light & Power",
"Pend Oreille PUD", "Peninsula Light Company", "Port Angeles City Light",
"Puget Sound Energy", "Seattle City Light", "Skamania PUD", "Snohomish County PUD",
"Tacoma Public Utilities", "Tanner Electric Cooperative", "Town of Eatonville",
"Town of Ruston", "Town of Steilacoom", "Vera Water & Power",
"Wahkiakum County PUD", "Whatcom County PUD", "Big Bend Electric",
"Northern Lights Inc"), InData = c(0L, 1L, 0L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L,
0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L), UtilityType = c("Coop", "Private", "Public",
"Public", "Public", "Public", "Public", "Public", "Public", "Public",
"Public", "Public", "Public", "Public", "Public", "Public", "Public",
"Public", "Coop", "Coop", "Public", "Public", "Coop", "Public",
"Public", "Public", "Public", "Coop", "Public", "Public", "Public",
"Coop", "Coop", "Public", "Public", "Public", "Public", "Coop",
"Coop", "Public", "Coop", "Public", "Private", "Coop", "Public",
"Coop", "Public", "Private", "Public", "Public", "Public", "Public",
"Coop", "Public", "Public", "Public", "Coop", "Public", "Public",
"Coop", "Coop"), County1 = c("Pierce", "Whitman", "Asotin", "Benton",
"Whatcom", "Lewis", "Chelan", "Chelan", "Stevens", "Spokane",
"Grant", "Kittitas", "Grays Harbor", "Fulton County", "Benton County",
"Whatcom", "Clallam", "Clark", "Asotin ", "Walla Walla", "Cowlitz",
"Douglas", "Pierce", "Ferry", "Franklin", "Grant", "Grays Harbor",
"Spokane", "Jefferson", "Kittitas", "Klickitat", "Spokane", "Grant",
"Lewis", "Mason", "Mason", "Spokane", "Okanogan", "Pierce", "Okanogan",
"San Juan", "Pacific", "Yakima", "Pierce", "Pend Oreille", "Pierce",
"Clallam", "Island", "King", "Skamania", "Snohomish", "Pierce",
"King", "Pierce", "Pierce", "Pierce", "Spokane", "Wahkiakum",
"Whatcom", "Adams", "Pend Oreille"), County2 = c("", "Spokane",
"", "", "", "", "", "", "", "", "Douglas", "", "", "", "", "",
"", "", "Whitman", "Columbia", "", "", "", "Okanogan", "", "",
"", "Whitman", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "Garfield", "", "", "", "", "King", "", "", "Island",
"Mason", "Pierce", "", "", "", "", "", "", "", ""), County3 = c("",
"Stevens", "", "", "", "", "", "", "", "", "Okanogan", "", "",
"", "", "", "", "", "Garfield", "", "", "", "", "", "", "", "",
"Lincoln", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "Columbia", "", "", "", "", "Kitsap", "", "", "", "Grays Harbor",
"", "", "", "", "", "", "", "", ""), County4 = c("", "Lincoln",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "Stevens", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "Walla Walla", "", "",
"", "", "Kittitas", "", "", "", "Lewis", "", "", "", "", "",
"", "", "", ""), County5 = c("", "Asotin", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "Garfield", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "Benton", "", "", "", "", "Pierce", "", "",
"", "", "", "", "", "", "", "", "", "", ""), County6 = c("",
"Adams", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "Adams", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "Kittitas",
"", "", "", "", "Skagit", "", "", "", "", "", "", "", "", "",
"", "", "", ""), County7 = c("", "Ferry", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "Columbia", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "Thurston", "", "", "",
"", "", "", "", "", "", "", "", "", ""), County8 = c("", "Franklin",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "Pend Orielle", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"Whatcom", "", "", "", "", "", "", "", "", "", "", "", "", ""
), County9 = c("", "Grant", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"Grant", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", ""), County10 = c("", "Pend Oreille", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "Asotin", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "")), row.names = c(NA, 61L
), class = "data.frame")
【问题讨论】:
-
请使用
dput(df)的输出分享您的数据集的样例。另外,分享一下转换后的预期输出。 -
我补充了,谢谢!