各位好,我是刚接触r的小白一个。我想要合并两个dataframe,但是发现merge后的结果只是把两个dataframe上下连结在一起。下面是我的code:
h=read.csv("h1b_kaggle.csv")
##https://www.kaggle.com/nsharan/h-1b-visa/data
h_1=h
#summary(h_1)
#str(h_1)
##separate the city and state
library(stringr)
a=str_split_fixed(h_1$WORKSITE, ",", 2)
h_position=data.frame(a)
names(h_position)[1] <- "City"
names(h_position)[2] <- "State"
library(dplyr)
h_1=h_1%>%mutate(CITY=h_position$City,STATE=h_position$State)
##map
##idea from:https://gist.github.com/cdesante/4252133
levels(h_1$STATE) <- tolower(levels(h_1$STATE))
w=table(h_1$STATE)
t = as.data.frame(w)
names(t)[1] = 'region'
library(maps)
library(ggplot2)
library(ggmap)
all_states <- map_data("state")
all_states$count <- t$Freq[match(all_states$region, t$Var1)]
## HERE is the problem:
##cannot merage 'all_states' and 't' on 'region'.
Total <- merge(all_states,t, by=c("region"),all= T)
下面是我得到的结果: