【发布时间】:2018-12-08 01:37:32
【问题描述】:
我有两个数据框。 Dataframe1 包含公园列表的气候数据。数据框 2 包含相同的公园,但有一个字母(“P”或“T”)来指定该公园内的一条道路。我想将气候数据与这些公园道路名称相匹配。例如。在数据框 1 中,“公园 A”可能有 25 厘米的降水。数据框 2 将具有“Park A P”和“Park A T”。我希望数据框 2 在“Park A P”和“Park A T”中的值都为 25。任何帮助,将不胜感激。
数据框1
structure(list(ParkName = c("Alafia River State Park", "Amelia Island State Park",
"Big Talbot Island State Park", "Blue Spring State Park", "Collier-Seminole State Park",
"Curry Hammock State Park", "Delnor-Wiggins Pass State Park",
"Dr. Von D. Mizell-Eula Johnson State Park", "Fakahatchee Strand Preserve State Park",
"Fort George Island Cultural State Park", "Fort Pierce Inlet State Park/Avalon State Park",
"Fort Zachary Taylor Historic State Park", "Highlands Hammock State Park",
"Hillsborough River State Park", "Honeymoon Island State Park",
"John D. MacArthur Beach State Park", "Jonathan Dickinson State Park",
"Lake Manatee State Park", "Long Key State Park", "Lovers Key State Park",
"Myakka River State Park", "Oscar Scherer State Park", "Paynes Creek Historic State Park",
"Pumpkin Hill Creek Preserve State Park", "Savannas Preserve State Park",
"Seabranch Preserve State Park", "Sebastian Inlet State Park",
"Terra Ceia Preserve State Park", "Werner-Boyce Salt Springs State Park"
), tmax = c(30.8666666666667, 26.2333333333333, 25.325, 29, 30.3166666666667,
29.1166666666667, 32.65, 30.7833333333333, 33, 26.3, 27.56, 30.6,
30.2333333333333, 33, 28.7666666666667, 28.98, 27.9142857142857,
27.4333333333333, 28.85, 29.4, 30.35, 32.7666666666667, 29.4,
30.1666666666667, 29.5166666666667, 32.4, 27.2583333333333, 29.7,
24.4333333333333), tavg = c(25.5, 21.6111111111111, 20.8333333333333,
23.5, 25.3166666666667, 25.9166666666667, 28, 26.9666666666667,
28.2, 22.025, 23.3, 28.6, 24.5833333333333, 27.7, 23.8333333333333,
24.62, 23.0714285714286, 21.5333333333333, 26, 24.32, 24.4166666666667,
28.0666666666667, 23.5333333333333, 25.9333333333333, 25.1666666666667,
28.2, 22.875, 24.86, 18.2), tmin = c(20.1333333333333, 17.0222222222222,
16.3666666666667, 18.02, 20.3166666666667, 22.6833333333333,
23.35, 23.2, 23.3, 17.75, 19.02, 26.6, 18.8833333333333, 22.45,
18.8666666666667, 20.28, 18.2285714285714, 15.6333333333333,
23.1, 19.24, 18.4166666666667, 23.3666666666667, 17.6333333333333,
21.7333333333333, 20.85, 23.95, 18.4416666666667, 20.04, 11.9333333333333
), precip = c(163.833333333333, 108.333333333333, 116.333333333333,
104.2, 156, 127.333333333333, 302, 168.166666666667, 259, 111,
117.8, 154, 131.833333333333, 208.5, 112.333333333333, 152.4,
94.2857142857143, 179.333333333333, 128.833333333333, 146, 147.666666666667,
223.666666666667, 137.333333333333, 122.333333333333, 154.166666666667,
226, 126.5, 137.8, 20.6666666666667), ParkLatitude = c(27.77339935,
30.52039909, 30.47949982, 28.94799995, 25.97559929, 24.74399948,
26.28140068, 26.07530022, 26.02120018, 30.42499924, 27.5222,
24.54649925, 27.4503994, 28.12179947, 28.07509995, 26.82620049,
27.00860023, 27.47850037, 24.81699944, 26.39119911, 27.22629929,
27.1807003, 27.61980057, 30.4701004, 27.3166008, 27.13529968,
27.86020088, 27.5977993, 28.31739998)), class = c("tbl_df", "tbl",
"data.frame"), .Names = c("ParkName", "tmax", "tavg", "tmin",
"precip", "ParkLatitude"), row.names = c(NA, -29L))
数据框 2
structure(list(newsites = c("Alafia River State Park T", "Blue Spring State Park P",
"Collier-Seminole State Park T", "Curry Hammock State Park P",
"Highlands Hammock State Park T", "Jonathan Dickinson State Park P",
"Jonathan Dickinson State Park T", "John D. MacArthur Beach State Park P",
"Dr. Von D. Mizell-Eula Johnson State Park P", "Long Key State Park P",
"Lovers Key State Park T", "Myakka River State Park T", "Myakka River State Park P",
"Savannas Preserve State Park P", "Seabranch Preserve State Park T",
"Sebastian Inlet State Park P", "Sebastian Inlet State Park T",
"Big Talbot Island State Park T", "Big Talbot Island State Park P",
"Fort George Island Cultural State Park T", "Amelia Island State Park T",
"Pumpkin Hill Creek Preserve State Park T", "Blue Spring State Park T",
"Collier-Seminole State Park P", "Delnor-Wiggins Pass State Park P",
"Fakahatchee Strand Preserve State Park T", "Fort Pierce Inlet State Park/Avalon State Park P",
"Fort Zachary Taylor Historic State Park P", "Highlands Hammock State Park P",
"Hillsborough River State Park P", "Honeymoon Island State Park P",
"Lake Manatee State Park T", "Oscar Scherer State Park P", "Paynes Creek Historic State Park P",
"Lake Manatee State Park P", "Lovers Key State Park P", "Terra Ceia Preserve State Park T",
"Werner-Boyce Salt Springs State Park T")), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -38L), .Names = "newsites", vars = "newsites", drop = TRUE, indices = list(
0L, 20L, 18L, 17L, 1L, 22L, 23L, 2L, 3L, 24L, 8L, 25L, 19L,
26L, 27L, 28L, 4L, 29L, 30L, 7L, 5L, 6L, 34L, 31L, 9L, 35L,
10L, 12L, 11L, 32L, 33L, 21L, 13L, 14L, 15L, 16L, 36L, 37L), group_sizes = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
newsites = c("Alafia River State Park T", "Amelia Island State Park T",
"Big Talbot Island State Park P", "Big Talbot Island State Park T",
"Blue Spring State Park P", "Blue Spring State Park T", "Collier-Seminole State Park P",
"Collier-Seminole State Park T", "Curry Hammock State Park P",
"Delnor-Wiggins Pass State Park P", "Dr. Von D. Mizell-Eula Johnson State Park P",
"Fakahatchee Strand Preserve State Park T", "Fort George Island Cultural State Park T",
"Fort Pierce Inlet State Park/Avalon State Park P", "Fort Zachary Taylor Historic State Park P",
"Highlands Hammock State Park P", "Highlands Hammock State Park T",
"Hillsborough River State Park P", "Honeymoon Island State Park P",
"John D. MacArthur Beach State Park P", "Jonathan Dickinson State Park P",
"Jonathan Dickinson State Park T", "Lake Manatee State Park P",
"Lake Manatee State Park T", "Long Key State Park P", "Lovers Key State Park P",
"Lovers Key State Park T", "Myakka River State Park P", "Myakka River State Park T",
"Oscar Scherer State Park P", "Paynes Creek Historic State Park P",
"Pumpkin Hill Creek Preserve State Park T", "Savannas Preserve State Park P",
"Seabranch Preserve State Park T", "Sebastian Inlet State Park P",
"Sebastian Inlet State Park T", "Terra Ceia Preserve State Park T",
"Werner-Boyce Salt Springs State Park T")), class = "data.frame", row.names = c(NA,
-38L), vars = "newsites", drop = TRUE, .Names = "newsites"))
【问题讨论】:
-
Dataframe1 和 Dataframe2 的
dputs 好像是一样的? -
感谢@MauritsEvers 我已经修正了错误!
-
差不多;-) 第一个代码块仍然包含两个
dputs。 -
好的!现在可以走了。对此感到抱歉,谢谢!
-
感谢您的澄清;我在下面添加了
tidyverse解决方案;请看一下。
标签: r pattern-matching string-matching