合并功能问题

时间:2020-03-11 20:59:05

标签: r

我正在尝试创建一个按州划分的Covid-19总病例的美国地图。为了完成该任务,我需要以下代码:

library(readxl)
library(maptools)
library(spdep)
library(RColorBrewer)

library(sp)
library(sf)

hp_usa_states <- readShapeSpatial("USA_States.shp")
names(shp_usa_states)                              
shp_usa_states@data 

infected <- read_excel("C:/Users/BP2646/Desktop/databaseinfUS/US.xlsx") 
names(infected)      

我对这部分代码有疑问“ (fix.by(by.x,x)中的错误:'by'必须指定唯一有效的列)

infected_bystate <- shp_usa_states
    infected_bystate@data <- merge(shp_usa_states@data,infected,by.x= "Cases",by.y="Cases",all.x = TRUE,sort  = FALSE)
    summary(infected_bystate)

我有这个问题:
fix.by(by.x,x)中的错误:'by'必须指定唯一有效的列

我不知道为什么。 这是我的数据集:

structure(list(State = c("Arizona", "California", "Colorado", 
"Connecticut", "District of Columbia", "Florida", "Georgia", 
"Hawaii", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky[384][385]", 
"Lousiana", "Maryland", "Massachusetts", "Michigan", "Minnesota", 
"Missouri", "Nebraska", "Nevada", "New Hampshire", "New Jersey", 
"New York", "North Carolina", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", 
"Rhode Island", "South Carolina", "Soth Dakota", "Tennessee", 
"Texas", "Utah", "Vermont", "Virginia", "Washington", "Wisconsin", 
"Total"), Cases = c(6, 157, 18, 2, 4, 28, 22, 2, 19, 6, 13, 1, 
8, 6, 9, 92, 2, 3, 1, 5, 4, 5, 15, 176, 7, 3, 2, 15, 14, 5, 9, 
5, 7, 21, 2, 1, 9, 275, 3, 976), Recovered = c(1, 6, 0, 0, 0, 
0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 12), Deceased = c(0, 3, 
0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 24, 0, 31), Remaining = c(5, 
148, 18, 2, 4, 26, 22, 2, 17, 6, 13, 1, 8, 6, 9, 91, 2, 3, 1, 
5, 4, 5, 14, 176, 7, 3, 2, 15, 14, 5, 9, 4, 7, 21, 2, 1, 9, 250, 
2, 933)), row.names = c(NA, -40L), class = c("tbl_df", "tbl", 
"data.frame"))

这是第二个数据帧:

structure(list(STATE_NAME = structure(c(48L, 27L, 20L, 35L, 42L, 
51L, 50L, 13L, 46L, 24L, 38L, 30L, 16L, 22L, 28L, 33L, 39L, 7L, 
40L, 31L, 15L, 29L, 45L, 5L, 36L, 14L, 9L, 8L, 49L, 21L, 6L, 
18L, 17L, 47L, 26L, 3L, 37L, 34L, 43L, 44L, 32L, 1L, 25L, 11L, 
41L, 4L, 19L, 10L, 23L, 12L, 2L), .Label = c("Alabama", "Alaska", 
"Arizona", "Arkansas", "California", "Colorado", "Connecticut", 
"Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", 
"Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", 
"Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", 
"Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", 
"Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", 
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", 
"Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", 
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", 
"West Virginia", "Wisconsin", "Wyoming"), class = "factor"), 
    STATE_FIPS = structure(c(48L, 27L, 20L, 35L, 42L, 51L, 50L, 
    13L, 46L, 24L, 38L, 30L, 16L, 22L, 28L, 33L, 39L, 7L, 40L, 
    31L, 15L, 29L, 45L, 5L, 36L, 14L, 9L, 8L, 49L, 21L, 6L, 18L, 
    17L, 47L, 26L, 3L, 37L, 34L, 43L, 44L, 32L, 1L, 25L, 11L, 
    41L, 4L, 19L, 10L, 23L, 12L, 2L), .Label = c("01", "02", 
    "04", "05", "06", "08", "09", "10", "11", "12", "13", "15", 
    "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", 
    "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
    "36", "37", "38", "39", "40", "41", "42", "44", "45", "46", 
    "47", "48", "49", "50", "51", "53", "54", "55", "56"), class = "factor"), 
    STATE_ABBR = structure(c(48L, 27L, 22L, 29L, 42L, 51L, 49L, 
    14L, 47L, 24L, 38L, 31L, 13L, 20L, 30L, 35L, 39L, 7L, 40L, 
    32L, 16L, 34L, 45L, 5L, 36L, 15L, 8L, 9L, 50L, 21L, 6L, 18L, 
    17L, 46L, 25L, 4L, 37L, 28L, 43L, 44L, 33L, 2L, 26L, 11L, 
    41L, 3L, 19L, 10L, 23L, 12L, 1L), .Label = c("AK", "AL", 
    "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", 
    "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", 
    "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", 
    "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", 
    "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY"), class = "factor")), class = "data.frame", row.names = c("0", 
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", 
"24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", 
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", 
"46", "47", "48", "49", "50"), data_types = c("C", "C", "C"))

0 个答案:

没有答案