【问题标题】:Filling in a dataframe from another dataframe using partial string matching使用部分字符串匹配从另一个数据帧填充数据帧
【发布时间】: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


【解决方案1】:

假设要匹配的两个 character 字符串之间的唯一区别是空格分隔的单个字符,这里是 tidyverse 替代使用 dplyr::left_join

library(tidyverse);
df2 %>%
    ungroup() %>%
    mutate(ParkName = gsub("\\s[A-Za-z]$", "", newsites)) %>%
    left_join(df1, by = "ParkName") %>%
    select(newsites, precip)
## A tibble: 38 x 2
#   newsites                                    precip
#   <chr>                                        <dbl>
# 1 Alafia River State Park T                    164.
# 2 Blue Spring State Park P                     104.
# 3 Collier-Seminole State Park T                156.
# 4 Curry Hammock State Park P                   127.
# 5 Highlands Hammock State Park T               132.
# 6 Jonathan Dickinson State Park P               94.3
# 7 Jonathan Dickinson State Park T               94.3
# 8 John D. MacArthur Beach State Park P         152.
# 9 Dr. Von D. Mizell-Eula Johnson State Park P  168.
#10 Long Key State Park P                        129.
## ... with 28 more rows

请注意,df1 是您的“数据框 1”,df2 是您的“数据框 2”。

【讨论】:

  • 当然是更通用的解决方案 1+
  • 嗨@MauritsEvers,我对您在tidyverse 管道中的变异代码有疑问。我开始了解 gsub,所以我知道“\\s”表示空格,然后 [A-Za-z] 表示所有大写和小写字母。但是,最后的“$”是什么意思?再次感谢!
  • @LeoOhyama "$" 是字符串尾锚;例如,"T$" 将匹配 last 字符为 "T" 的字符串。
【解决方案2】:
# small data frames for illustration purposes
df1 <- data.frame(park = c('Yellowstone', 'Zion', 'Redwood'), rain = c(1,2,3), stringsAsFactors = F)
df2 <- data.frame(road = c('Yellowstone P', 'Yellowstone T', 'Zion P', 'Zion T', 'Redwood P', 'Redwood T'), stringsAsFactors = F)

# Make a new column in both data frames which only contains the first 4 characters of the park and road names
df1 <- cbind(df1, shortName = substring(df1$park, 1, 4))
df2 <- cbind(df2, shortName = substring(df2$road, 1, 4))

# Merge df1 and df2 according to the new shortName column we made
df3 <- merge(x = df1, y = df2, by = 'shortName')

df3
#  shortName        park rain          road
#1      Redw     Redwood    3     Redwood P
#2      Redw     Redwood    3     Redwood T
#3      Yell Yellowstone    1 Yellowstone P
#4      Yell Yellowstone    1 Yellowstone T
#5      Zion        Zion    2        Zion P
#6      Zion        Zion    2        Zion T

【讨论】:

    猜你喜欢
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 2021-08-11
    • 2014-09-20
    • 2016-12-10
    • 1970-01-01
    相关资源
    最近更新 更多