如何在 R 中执行 VLOOKUP(类似于 Excel)?
VLOOKUP 是 excel 中的一个函数,它是垂直查找的缩写。此函数的任务是在列中搜索特定值以从不同列但在同一行中返回值。
句法:
VLOOKUP([value], [range], [column no], [true/false])
Here,
- value: Specifies the value to be searched
- range: It specifies the range in which the value has to be searched
- column no: The number of the column that contains the return value
- true: If the user wants approximate match
- false: If the user wants the exact match with the specified value
方法一:使用Base R进行VLOOKUP:
我们可以使用 merge()函数在基础 R 中实现 VLOOKUP。
句法:
merge(dataFrame1, dataFrame2, by = “columnName”)
Here,
- dataFrame1 and dataFrame2 are the dataFrames and by argument is optional and used to specify multiple columns to merge
例子:
在这个程序中,首先,我们创建了两个数据框。然后我们应用了合并函数。请注意,我们在两个数据框中相同的部分列的基础上合并了列。
R
# R program to perform VLOOKUP
# using merge function
# creating a dataframe
dataFrame1 < - data.frame(section=LETTERS[1:15],
team=rep(c('Alpha', 'Beta', 'Gamma'),
each=5))
# creating another dataframe
dataFrame2 < - data.frame(section=LETTERS[1:15],
score=c(25, 13, 12, 16, 18, 19,
26, 28, 20, 36, 44, 29,
8, 6, 5))
# merge the two dataframes
merge(dataFrame1, dataFrame2, by="section")
R
# R program to perform VLOOKUP
# using dplyr
# Including library
library(dplyr)
# creating a dataframe
dataFrame1 <- data.frame(section=LETTERS[1:15],
team=rep(c('Alpha', 'Beta', 'Gamma'),
each=5))
# creating another dataframe
dataFrame2 <- data.frame(section=LETTERS[1:15],
score=c(25, 13, 12, 16, 18, 19,
26, 28, 20, 36, 44, 29,
8, 6, 5))
# merging the two dataframes by using
# inner_join function
inner_join(dataFrame1, dataFrame2, by="section")
输出:
方法二:使用 dplyr 执行 VLOOKUP
我们可以使用 R 中 dplyr 库的内连接函数来执行类似于 VLOOKUP函数。
句法:
inner_join(dataFrame1, dataFrame2, by=”columnName”)
Here,
- dataFrame1 and dataFrame2 are the dataFrames and by argument is optional and used to specify multiple columns to merge
安装和导入 dplyr 包的语法:
install.package('dplyr')
library(dplyr)
例子:
在这个程序中,首先,我们创建了两个数据框。然后我们应用了 inner_join函数。请注意,我们在两个数据框中相同的部分列的基础上合并了列。
R
# R program to perform VLOOKUP
# using dplyr
# Including library
library(dplyr)
# creating a dataframe
dataFrame1 <- data.frame(section=LETTERS[1:15],
team=rep(c('Alpha', 'Beta', 'Gamma'),
each=5))
# creating another dataframe
dataFrame2 <- data.frame(section=LETTERS[1:15],
score=c(25, 13, 12, 16, 18, 19,
26, 28, 20, 36, 44, 29,
8, 6, 5))
# merging the two dataframes by using
# inner_join function
inner_join(dataFrame1, dataFrame2, by="section")
输出: