0

Here I was using 2 datasets "dailyActivity_merged" and "sleepDay_merged" which had column names "ActivityDate" and "SleepDay" which had inputs of data in them, so i wanted to merge these datasets together by "Id" and "Date" after renaming the respective columns as "Date" as shown in the following code:

rename(dailyActivity_merged,Date=Avtivitydate)
rename(sleepDay_merged,Date=SleepDay)
merge(dailyActivity_merged,sleepDay_merged,by=c("Id","Date"),all.x=TRUE)

But I kept getting the following error:

Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column

I initially thought it was because of some inconsistency in "Id" but that was not the case as I ran the merge function without "Date" and it was running. If anyone could guide me here I would be grateful.

  • Can you post sample data? Please edit the question with the output of `dput` for each data set, something like `dput(head(df))`. – Rui Barradas Jan 02 '23 at 06:26
  • 3
    Did you assign the renamed data to a variable? – Martin Gal Jan 02 '23 at 06:50
  • 2
    As hinted by Martin, `rename` (and most R functions) do not operate *in place*, that is, the object passed to the function is not modified, but a copy is created. You probably want `dailyActivity_merged <- rename(dailyActivity_merged,Date=Avtivitydate)` etc – Aurèle Jan 02 '23 at 07:54

1 Answers1

-1

Use full_join(df1,df2,by="Id") You don't even need to explicitly state by "id" but it's a good habbit.

Otherwise read documentation for inner-join and right_join Install the tidyverse packages to make use of this code

RYann
  • 567
  • 1
  • 7
  • The packages you are using are missing and `"I'd"` and `"id"` aren't the same. – Martin Gal Jan 02 '23 at 10:27
  • It's the auto correct. Op should use the tidyverse packages. That is a bit of an obvious to me... – RYann Jan 02 '23 at 13:33
  • It's obvious to you since you know these packages but those aren't part of `base R`. See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Both issues mentioned in my comment could easily corrected. – Martin Gal Jan 02 '23 at 13:42