1

I was looking for an R equivalent function to the vlookup function in Excel and I came across lookup in R, however when I try to use it I keep getting errors.

I have 2 data frames (myresday and Assorted). myresday contains 2 columns: one with codes (column name is Res.Code) and the other with corresponding days of the week (colname = ContDay). Each of the codes represents a person and each person is matched with a day of the week they are supposed to be in work. Assorted contains the record of when each person actually came in over the course of a year. It is a dataframe similar to myresday, however it is much bigger. I want to see if the codes in Assorted are matched with the correct days or if the days corresponding to each code is incorrect.

I was trying to use lookup but kept coming across several errors. Here is my code:

Assorted$Cont_Day <- lookup(Assorted$VISIT_PROV_ID, myresday[, 1:2]) 
'the codes in myresday are in column 1, the days in column 2 

R kept saying the function couldn't be found. I looked more into the function and someone suggested to use qdapTools library, so I put:

library('qdapTools')

before my code, and it said there is not qdapTools package.
Does anyone know how to do this or know of a better way to solve this?

Cam
  • 421
  • 2
  • 8
  • 18
  • I'd guess that the equivalent of `vlookup` in R is just `merge`? Take a look [here](http://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – David Arenburg Aug 06 '15 at 14:19

2 Answers2

3

you need to install qdapTools before using its library. using this code works:

install.packages("qdapTools")
library('qdapTools')
Assorted$Cont_Day <- lookup(Assorted$VISIT_PROV_ID, myresday[, 1:2])
Cam
  • 421
  • 2
  • 8
  • 18
1

The base library function merge is likely to do what you need it to do, without involving any packages.

Let's make some toy data

set.seed(100)
myresday <- data.frame(
  Res.Code=1:30,
  ContDay=sample(1:7, 30, replace=T))
Assorted <- data.frame(
  date=sample(seq(as.Date('2010-01-1'),as.Date('2011-01-01'), by='day'), 100, replace=T),
  VISIT_PROV_ID=sample(1:30, 100, replace=T))
head(Assorted)
        date VISIT_PROV_ID
1 2010-06-28             8
2 2010-12-06            26
3 2010-05-08            23
4 2010-12-16            15
5 2010-09-12            18
6 2010-11-22             1

And then do the merge

checkDay <- merge(Assorted, myresday, by.x='VISIT_PROV_ID', by.y='Res.Code')
head(checkDay)
  VISIT_PROV_ID       date ContDay
1             1 2010-06-16       3
2             1 2010-08-07       3
3             1 2010-11-22       3
4             1 2010-03-18       3
5             2 2010-08-19       2
6             2 2010-11-04       2

Edit: Updated column names

user295691
  • 7,108
  • 1
  • 26
  • 35