我有一个数据集,它在同一列中包含参数、日期和值。我正在寻找一种方法来匹配它们并创建单独的列。我试图让每个不同参数的最终结果如下所示:
df<-structure(list(`Date & Time` = c(NA, NA, "Minimum", "MinDate",
"Maximum", "MaxDate", "Avg", "Num", "Data[%]", "STD"), Bayonne...2 = c("WSPD",
"mph", "1.1", "1/1/2019 1:00 AM", "22.4", "1/21/2019 7:00 PM",
"8.3", "743", "99.8", "4.7"), Bayonne...3 = c("WDIR", "Deg",
"20", "1/5/2019 4:00 PM", "337", "1/27/2019 11:00 PM", "206",
"743", "99.8", "100.7"), Bayonne...4 = c("TEMP", "DegF", "3",
"1/31/2019 4:00 AM", "59", "1/24/2019 12:00 PM", "32", "743",
"99.8", "10"), Bayonne...5 = c("RH", "%", "27", "1/7/2019 6:00 PM",
"88.2", "1/1/2019 4:00 AM", "55.5", "743", "99.8", "15.1"), Bayonne...6 = c("BP",
"in HG", "29.21", "1/20/2019 10:00 AM", "30.64", "1/22/2019 9:00 AM",
"30.04", "743", "99.8", "0.2"), Bayonne...7 = c("RAIN", "in",
"0", "1/1/2019 2:00 AM", "0.286", "1/24/2019 10:00 AM", "2.946",
"743", "99.8", "No Data"), `Camden Spruce St...8` = c("WSPD",
"mph", "0.6", "1/18/2019 1:00 PM", "21.2", "1/10/2019 2:00 PM",
"6.2", "744", "100", "4.3"), `Camden Spruce St...9` = c("WDIR",
"Deg", "27", "1/5/2019 9:00 AM", "332", "1/10/2019 8:00 PM",
"205", "744", "100", "95.5"), `Camden Spruce St...10` = c("TEMP",
"DegF", "6", "1/31/2019 2:00 AM", "60", "1/1/2019 9:00 AM", "33",
"744", "100", "10.1"), `Camden Spruce St...11` = c("RH", "%",
"24.5", "1/12/2019 2:00 PM", "89.3", "1/1/2019 12:00 AM", "52.9",
"744", "100", "17.6"), `Camden Spruce St...12` = c("BP", "in HG",
"29.22", "1/20/2019 9:00 AM", "30.69", "1/22/2019 10:00 AM",
"30.06", "744", "100", "0.2"), `Camden Spruce St...13` = c("RAIN",
"in", "0", "1/1/2019 2:00 AM", "0.233", "1/19/2019 11:00 PM",
"3.249", "744", "100", "No Data"), Columbia...14 = c("WSPD",
"mph", "0.5", "1/28/2019 6:00 PM", "14", "1/29/2019 11:00 PM",
"3.5", "743", "99.8", "2.7"), Columbia...15 = c("WDIR", "Deg",
"69", "1/1/2019 1:00 AM", "341", "1/1/2019 1:00 AM", "250", "743",
"99.8", "58.4"), Columbia...16 = c("TEMP", "DegF", "-3", "1/31/2019 11:00 PM",
"56", "1/24/2019 11:00 AM", "27", "744", "100", "10.6"), Columbia...17 = c("RH",
"%", "26.2", "1/25/2019 5:00 PM", "91.9", "1/1/2019 5:00 AM",
"60", "744", "100", "16.7"), Columbia...18 = c("BP", "in HG",
"28.77", "1/20/2019 8:00 AM", "30.11", "1/22/2019 9:00 AM", "29.51",
"744", "100", "0.2"), Columbia...19 = c("RAIN", "in", "0", "1/1/2019 1:00 AM",
"0.386", "1/5/2019 10:00 AM", "3.351", "744", "100", "No Data"
), `Elizabeth Trailer...20` = c("WSPD", "mph", "0.7", "1/27/2019 1:00 AM",
"20", "1/21/2019 7:00 AM", "5", "740", "99.4", "2.7"), `Elizabeth Trailer...21` = c("WDIR",
"Deg", "34", "1/1/2019 1:00 AM", "330", "1/1/2019 1:00 AM", "228",
"744", "100", "81.7"), `Elizabeth Trailer...22` = c("TEMP", "DegF",
"2", "1/31/2019 3:00 AM", "58", "1/1/2019 8:00 AM", "32", "744",
"100", "10.6"), `Elizabeth Trailer...23` = c("RH", "%", "24.7",
"1/22/2019 3:00 PM", "89.7", "1/1/2019 3:00 AM", "52.3", "744",
"100", "17.1"), `Elizabeth Trailer...24` = c("BP", "in HG", "29.2",
"1/20/2019 10:00 AM", "30.63", "1/22/2019 9:00 AM", "30.03",
"744", "100", "0.2"), `Elizabeth Trailer...25` = c("RAIN", "in",
"0", "1/1/2019 2:00 AM", "0.222", "1/20/2019 12:00 AM", "2.812",
"744", "100", "No Data"), Flemington...26 = c("BP", "in HG",
"29.07", "1/20/2019 9:00 AM", "30.5", "1/22/2019 9:00 AM", "29.89",
"743", "99.8", "0.2"), Flemington...27 = c("RH", "%", "26.7",
"1/11/2019 2:00 PM", "92.7", "1/1/2019 3:00 AM", "59.9", "743",
"99.8", "18.2"), Flemington...28 = c("SOLAR", "Langleys", "No Data",
"1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM", "No Data",
"0", "0", "No Data"), Flemington...29 = c("TEMP", "DegF", "0",
"1/31/2019 6:00 AM", "59", "1/24/2019 10:00 AM", "29", "743",
"99.8", "10.6"), Flemington...30 = c("WSPD", "mph", "0.6", "1/31/2019 9:00 PM",
"12.3", "1/24/2019 9:00 AM", "3.7", "743", "99.8", "2.6"), Flemington...31 = c("WDIR",
"Deg", "69", "1/5/2019 6:00 AM", "317", "1/6/2019 8:00 PM", "211",
"743", "99.8", "63.6"), Flemington...32 = c("RAIN", "in", "0",
"1/1/2019 2:00 AM", "0.234", "1/24/2019 1:00 PM", "2.775", "743",
"99.8", "No Data"), `Fort Lee Near Road...33` = c("WSPD", "mph",
"1.2", "1/15/2019 7:00 PM", "18", "1/21/2019 5:00 PM", "5.9",
"744", "100", "3.2"), `Fort Lee Near Road...34` = c("WDIR", "Deg",
"68", "1/13/2019 9:00 PM", "306", "1/20/2019 12:00 PM", "228",
"744", "100", "65.6"), `Fort Lee Near Road...35` = c("TEMP",
"DegF", "0", "1/31/2019 5:00 AM", "57", "1/1/2019 10:00 AM",
"30", "744", "100", "10.3"), `Fort Lee Near Road...36` = c("RH",
"%", "26.9", "1/25/2019 9:00 PM", "90.6", "1/1/2019 2:00 AM",
"54.3", "744", "100", "17"), `Fort Lee Near Road...37` = c("BP",
"in HG", "28.9", "1/20/2019 10:00 AM", "30.29", "1/22/2019 10:00 AM",
"29.7", "744", "100", "0.2"), `Fort Lee Near Road...38` = c("RAIN",
"in", "0", "1/1/2019 2:00 AM", "0.244", "1/24/2019 10:00 AM",
"3.507", "744", "100", "No Data"), `Newark Firehouse...39` = c("BP",
"in HG", "No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data"), `Newark Firehouse...40` = c("RH",
"%", "No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data"), `Newark Firehouse...41` = c("SOLAR",
"Langleys", "0", "1/1/2019 12:00 AM", "0.758", "1/25/2019 12:00 PM",
"0.097", "744", "100", "0.1"), `Newark Firehouse...42` = c("TEMP",
"DegF", "No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data"), `Newark Firehouse...43` = c("WSPD",
"mph", "No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data"), `Newark Firehouse...44` = c("WDIR",
"Deg", "No Data", "1/1/2019 1:00 AM", "No Data", "1/1/2019 1:00 AM",
"No Data", "0", "0", "No Data"), `Newark Firehouse...45` = c("RAIN",
"in", "No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data"), `Rider University...46` = c("RH",
"%", "26", "1/11/2019 3:00 PM", "93.1", "1/1/2019 2:00 AM", "58.8",
"737", "99", "17.9"), `Rider University...47` = c("TEMP", "DegF",
"2", "1/31/2019 12:00 AM", "60", "1/1/2019 8:00 AM", "30", "737",
"99", "10.9"), `Rider University...48` = c("WSPD", "mph", "0.3",
"1/26/2019 7:00 PM", "17.1", "1/20/2019 5:00 PM", "4.5", "737",
"99", "3.8"), `Rider University...49` = c("WDIR", "Deg", "104",
"1/1/2019 1:00 AM", "333", "1/1/2019 1:00 AM", "244", "737",
"99", "61.5"), `Rider University...50` = c("SOLAR", "Langleys",
"No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data"), `Rider University...51` = c("BP",
"in HG", "29.11", "1/20/2019 9:00 AM", "30.56", "1/22/2019 10:00 AM",
"29.94", "737", "99", "0.2"), `Rider University...52` = c("RAIN",
"in", "0", "1/1/2019 2:00 AM", "0.317", "1/19/2019 11:00 PM",
"3.262", "737", "99", "No Data"), `Rutgers University...53` = c("BP",
"in HG", "No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data"), `Rutgers University...54` = c("TEMP",
"DegF", "No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data"), `Rutgers University...55` = c("RH",
"%", "No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data"), `Rutgers University...56` = c("RAIN",
"in", "No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data"), `Rutgers University...57` = c("SOLAR",
"watts/m2", "No Data", "1/1/2019 12:00 AM", "No Data", "1/1/2019 12:00 AM",
"No Data", "0", "0", "No Data")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
这是我迄今为止尝试过的,但我一直坚持如何走得更远:
df2<-df%>%
gather(Site,Value,2:57)%>%
mutate_if(str_detect(., 'Bayonne|Camden Spruce St|Columbia|Elizabeth Trailer|
Rider University|Rutgers University|Fort Lee Near Road|
Flemington|Newark Firehouse'),
~str_replace_all(., c("Bayonne.*" = "Bayonne",
"Camden Spruce St.*" = "Camden Spruce St",
"Columbia.*" = "Columbia",
"Elizabeth Trailer.*"="Elizabeth Trailer",
"Rider University.*" = "Rider University",
"Flemington.*" = "Flemington",
"Fort Lee Near Road.*" = "Fort Lee Near Road",
"Rutgers University.*" = "Rutgers University",
"Newark Firehouse.*" = "Newark Firehouse")))