官术网_书友最值得收藏!

  • Hands-On Data Science with R
  • Vitor Bianchi Lanzetta Nataraj Dasgupta Ricardo Anjoleto Farias
  • 312字
  • 2021-06-10 19:12:35

Merging DataFrames

R also provides a built-in command, merge, in order to join DataFrames. Let us create a separate DataFrame with the geographic location of the center of each state (Latitude and Longitude):

state2 <- data.frame(State=state.name, Latitude=state.center$y, Longitude=state.center$x) 
 
# The syntax for merge is as follows: 
 
## S3 method for class 'data.frame' 
# merge(x, y, by = intersect(names(x), names(y)), 
#       by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all, 
#       sort = TRUE, suffixes = c(".x",".y"), 
#       incomparables = NULL, ...) 
 
# Arguments 
 
# x and y = the DataFrames we want to merge 
# by = the column names by which we want to perform the merge 
 
# Note that since this can be different (if say the same column had different names in different DataFrames), we can use by.x and by.y to specify the corresponding name of the column across the 2 DataFrames 
 
# all = whether to keep rows that did not match in either in x or y  
# Using all=T means keep all rows of both the DataFrames even if there was no match 
 
merged <- merge(state, state2, by="State", all=T) 
merged 

Output is as follows:

New rows and columns can be added using the rbind and cbind functions.

For example, if, say, we create two DataFrames using a subset of the state DataFrame, we can combine them as follows:

# Using cbind to combine 2 separate data.frame of 2 columns each 
state0 <- state[,c(1:2)] 
state1 <- state[,c(3:4)] 
 
dim(state0) 
dim(state1) 
 
state01 <- cbind(state0,state1) 
state01 
 
# Population Income Illiteracy Life.Exp 
# Alabama              3615   3624        2.1    69.05 
# Alaska                365   6315        1.5    69.31 
# Arizona              2212   4530        1.8    70.55 
# Arkansas             2110   3378        1.9    70.66 
 
# Using rbind to combine 2 separate data.frame of 2 rows each 
state0 <- state[c(1:2),] 
state1 <- state[c(3:4),] 
 
dim(state0) 
dim(state1) 
 
state01 <- rbind(state0,state1) 
state01 

Output is as follows:

主站蜘蛛池模板: 营山县| 独山县| 沭阳县| 法库县| 桃园县| 大田县| 禄劝| 中山市| 谢通门县| 巩义市| 沽源县| 鄄城县| 治多县| 东阳市| 确山县| 南投市| 崇明县| 新化县| 西和县| 天长市| 贡觉县| 深泽县| 淮安市| 开远市| 蓝山县| 昭通市| 会理县| 瑞丽市| 达日县| 红河县| 蕲春县| 胶南市| 南通市| 农安县| 偏关县| 白银市| 台中市| 罗江县| 亚东县| 枞阳县| 新源县|