0
library(dplyr)
library(readr)
library(tidyverse)
library(tidyr)

I have 2 seperate dataframes, and they have one column in common. I need information from the columns with birthday and sex, to get thrown into the other dataframe. Some sample data: whiskey_drinkage:

person <- c('asdfe', 'twlkjdo', 'threwerlkqje', 'foudafljkr', 'fivwnerlke', 'sialkdsfjx', 'sevnvreen', 'eigvjnsht', 'ninvwjkne', 'trvnjn')
first_whiskey <- c('1990-12-24', '1989-01-01', '1972-05-20', '2000-01-01', '1999-12-31', '2019-02-28', '2004-04-23', '2008-08-08', '1999-02-19', '2015-08-09')
reason <- c('doomsday', 'doomsday', 'doomsday', 'doomsday', 'doomsday', 'sad', 'doomsday', 'doomsday', 'doomsday', 'doomsday')

whiskey_drinkage <- data.frame(person,first_whiskey,reason)

the following is because i don't know how to save dates as dates during data.frame buildup (!!!but that's not the important part here!!!).

whiskey_drinkage$first_whiskey <- whiskey_drinkage$first_whiskey %>% as.Date.character(whiskey_drinkage, format="%Y-%m-%d")

data2:

person <- c('asdfe', 'twlkjdo', 'threwerlkqje', 'foudafljkr', 'fivwnerlke', 'sialkdsfjx', 'sevnvreen', 'eigvjnsht', 'ninvwjkne', 'trvnjn', 'asdfwfe', 'twlkqerwdo', 'threwerewqje', 'foudaerwljkr', 'fivwhfglke', 'sifghdkdsfjx', 'dfghd', 'dfghdfgs', 'adfssdqqqq', 'dfksldkf')
sex <- c('M', 'M', 'F', 'F', 'M', 'M', 'F', 'F', 'F', 'M', 'M', 'M', 'F', 'F', 'M', 'M', 'F', 'F', 'F', 'M')
birthday <- c('1932-02-24', '1979-01-01', '1932-07-12', '1900-01-01', '1994-03-13', '2000-01-01', '1945-05-05', '1980-08-08', '1979-01-11', '2000-04-01', '1932-02-24', '1979-01-01', '1932-07-12', '1900-01-01', '1994-03-13', '2000-01-01', '1945-05-05', '1980-08-08', '1979-01-11', '2000-04-01')

data2 <- data.frame(person,sex,birthday)

Same story, still not important part()

data2$birthday <- data2$birthday %>% as.Date.character(data2, format="%Y-%m-%d")
head(whiskey_drinkage,10)
head(data2,10)

Now i want to do, is to take the information "sex" and "birthday" from data2 BUT ONLY from the people who are already registrered in "whiskey_drinkage". goal is to end up with the following dataset:

person <- c('asdfe', 'twlkjdo', 'threwerlkqje', 'foudafljkr', 'fivwnerlke', 'sialkdsfjx', 'sevnvreen', 'eigvjnsht', 'ninvwjkne', 'trvnjn')
first_whiskey <- c('1990-12-24', '1989-01-01', '1972-05-20', '2000-01-01', '1999-12-31', '2019-02-28', '2004-04-23', '2008-08-08', '1999-02-19', '2015-08-09')
reason <- c('doomsday', 'doomsday', 'doomsday', 'doomsday', 'doomsday', 'sad', 'doomsday', 'doomsday', 'doomsday', 'doomsday')
sex <- c('M', 'M', 'F', 'F', 'M', 'M', 'F', 'F', 'F', 'M')
birthday <- c('1932-02-24', '1979-01-01', '1932-07-12', '1900-01-01', '1994-03-13', '2000-01-01', '1945-05-05', '1980-08-08', '1979-01-11', '2000-04-01')

data3 <- data.frame(person, first_whiskey, reason, sex, birthday)
data3$birthday <- data3$birthday %>% as.Date.character(data3, format="%Y-%m-%d")
data3$first_whiskey <- data3$first_whiskey %>% as.Date.character(data3, format="%Y-%m-%d")
data3

IMPORTANT NOTE: in this example it's just the first 10 rows that are getting transfered, however in my real dataset, I have over 1000 different people registered, and the ones that needs to be extracted are scattered all over through the dataset.

1 Answers1

0

If I guess right, you can use left_join

`data3 <- data1 %>% left_join(data2, by='person')`
Ro Ma
  • 59
  • 6