Hi Paul,
Using the example provided by Ulrik, where
exdf1 <- data.frame(Date = c("1985-10-01", "1985-11-01", "1985-12-01?,
"1986-01-01"), Transits = c(NA, NA, NA, NA))
exdf2 <- data.frame(Date = c("1985-10-01", "1986-01-01"), Transits =
c(15,20)),
You could also try the following function:
for (i in 1:dim(exdf1)[1]){
if (!exdf1[i, 1] %in% exdf2[, 1]){
exdf2 <- rbind(exdf2, exdf1[i,])
}
}
Basically, what the function does is that it runs through the number of
rows in exdf1, and checks if the Date of the exdf1 row already exists in
Date column of exdf2. If so, it skips it. Otherwise, it binds the row to
df2.
Hope this helps!
Side note.: Computational efficiency wise, think Ulrik?s answer is
probably better. Presentation wise, his is also much better.
Regards,
Bo Lin
On 28 Mar 2017, at 5:22 PM, Ulrik Stervbo <ulrik.stervbo at gmail.com>
Hi Paul,
does this do what you want?
exdf1 <- data.frame(Date = c("1985-10-01", "1985-11-01", "1985-12-01",
"1986-01-01"), Transits = c(NA, NA, NA, NA))
exdf2 <- data.frame(Date = c("1985-10-01", "1986-01-01"), Transits =
20))
tmpdf <- subset(exdf1, !Date %in% exdf2$Date)
rbind(exdf2, tmpdf)
HTH,
Ulrik
On Tue, 28 Mar 2017 at 10:50 Paul Bernal <paulbernal07 at gmail.com> wrote:
Dear friend Mark,
Great suggestion! Thank you for replying.
I have two dataframes, dataframe1 and dataframe2.
dataframe1 has two columns, one with the dates in YYYY-MM-DD format and
other colum with number of transits (all of which were set to NA values).
dataframe1 starts in 1985-10-01 (october 1st 1985) and ends in 2017-03-01
(march 1 2017).
dataframe2 has the same two columns, one with the dates in YYYY-MM-DD
format, and the other column with number of transits. dataframe2 starts
have the same start and end dates, however, dataframe2 has missing dates
between the start and end dates, so it has fewer observations.
dataframe1 has a total of 378 observations and dataframe2 has a total of
362 observations.
I would like to come up with a code that could do the following:
Get the dates of dataframe1 that are missing in dataframe2 and add them
records to dataframe 2 but with NA values.
<dataframe1 <dataframe2
Date Transits Date
Transits
1985-10-01 NA 1985-10-01 15
1985-11-01 NA 1986-01-01 20
1985-12-01 NA 1986-02-01 5
1986-01-01 NA
1986-02-01 NA
2017-03-01 NA
I would like to fill in the missing dates in dataframe2, with NA as value
for the missing transits, so that I could end up with a dataframe3
as follows:
<dataframe3
Date Transits
1985-10-01 15
1985-11-01 NA
1985-12-01 NA
1986-01-01 20
1986-02-01 5
2017-03-01 NA
This is what I want to accomplish.
Thanks, beforehand for your help,
Best regards,
Paul
2017-03-27 15:15 GMT-05:00 Mark Sharp <msharp at txbiomed.org>:
Make some small dataframes of just a few rows that illustrate the
structure. Make a third that has the result you want. You will get an
answer very quickly. Without a self-contained reproducible problem,
vary.
Mark
R. Mark Sharp, Ph.D.
msharp at TxBiomed.org
On Mar 27, 2017, at 3:09 PM, Paul Bernal <paulbernal07 at gmail.com>
Dear friends,
I have one dataframe which contains 378 observations, and another one,
containing 362 observations.
Both dataframes have two columns, one date column and another one with
number of transits.
I wanted to come up with a code so that I could fill in the dates that
missing in one of the dataframes and replace the column of transits
the value NA.
I have tried several things but R obviously complains that the length
the dataframes are different.
How can I solve this?
Any guidance will be greatly appreciated,
Best regards,
Paul
[[alternative HTML version deleted]]