I am trying to combine 2 datasets of unequal length in a similar way to the example below for a longitudinal study. Dataset 1 includes each participant only once, with the row of data from their first weekly survey. Dataset 2 includes all surveys from all participants. I am trying to create a third dataset that accounts for missing weekly surveys. For example, if participant 2 missed their survey on the 17th of Jan, it will still show week 2, participant id and date with the rest of the cols blank. Any ideas on how to accomplish this are much appreciated as I am very new to R.
#dataframe 1 (many more value cols)
ID date value Weeknumber
1 March 1 8 1
2 Jan 10 9 1
3 April 12 12 1
4 Dec 9 6 1
#Dataframe 2
ID date value
1 March 1 8
1 March 8 3
1 March 15 9
1 March 22 11
1 March 29 5
2 Jan 10 9
2 Jan 24 5
2 Jan 31 12
2 Feb 7 7
3 April 12 12
3 April 19 3
3 April 26 10
3 May 2 6
4 Dec 9 6
4 Dec 30 7
4 Jan 6 11
#Desired output:
ID Date Value Week number
1 March 1 8 1
1 March 8 3 2
1 March 15 9 3
1 March 22 11 4
1 March 29 5 5
2 Jan 10 9 1
2 Jan 17 2
2 Jan 24 5 3
2 Jan 31 12 4
2 Feb 7 7 5
3 April 12 12 1
3 April 19 3 2
3 April 26 10 3
3 May 2 6 4
3 May 9 5
4 Dec 9 6 1
4 Dec 16 2
4 Dec 23 3
4 Dec 30 7 4
4 Jan 6 11 5
A possible way to do this is by using the function "match". But you need a proper map that map one value to the other. Let's make an example. I generate a data frame of random letters on a column and number on the other:
Now I want to add another column by using the match function. So I generate my "map", which is another dataframe that says which ones are to wovels.
Take in mind that this map is NOT complete, in fact, it doesn't map the consonants.
I can then use "match". "match" return the position in the second argument for each element of the first argument. We can, therefore, use these positions to call the elements in the column adf2$is_vocal and assign this to a new column in adf.
The many "NA" is due to the fact that there is no correspondence between consonants and the adf2 data frame.