0

I have one data frame df1 which contains the mean of heart rate (HR_mean) and rr (RR_mean) of one person taken from the experiment session 1:

  heart.rate  rr  HR_mean  RR_mean
  86         790  83.4     801.8
  84         828  83.4     801.8
  84         767  83.4     801.8
  82         811  83.4     801.8
  81         813  83.4     801.8

I have another data frame df2 with empty columns HR_mean and RR_mean:

  Person   Session  HR_mean  RR_mean
  Person1    1       
  Person1    2  
  Person2    1 
  Person2    2  
  Person3    1

I want to copy the values of HR_mean and RR_mean in df1 to the HR_mean and RR_mean columns in df2 like this:

df2:

Person   Session  HR_mean  RR_mean
Person1    1       83.4     801.8
Person1    2  
Person2    1 
Person2    2  
Person3    1
Greg
  • 1
  • What do you want empty values in the remaining rows to be? If you want them to be blank, all the values in both the columns will be `characters`. – Jason Mathews Jun 16 '19 at 03:49
  • 1
    Possible duplicate of [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – NelsonGon Jun 16 '19 at 05:03
  • Do you need `df2$HR_mean <- df1$HR_mean` and `df2$RR_mean <- df1$RR_mean` ? – Ronak Shah Jun 16 '19 at 07:57

1 Answers1

0

If I've understood the question correctly, here is a possible solution in dplyr. I've set the missing values in df2 to NA.

library(tidyverse)

df1 <- tribble(
~heart.rate,  ~rr,  ~HR_mean,  ~RR_mean,
86,         790,  83.4,     801.8,
84,         828,  83.4,     801.8,
84,         767,  83.4,     801.8,
82,         811,  83.4,     801.8,
81,         813,  83.4,     801.8)



df2 <- tribble(
~Person,   ~Session,  ~HR_mean,  ~RR_mean,
'Person1',    1, NA, NA,  
'Person1',    2, NA, NA,
'Person2',    1, NA, NA,
'Person2',    2, NA, NA,
'Person3',    1, NA, NA)


df2 <- df2 %>% 
  mutate(HR_mean = df1$HR_mean,
         RR_mean = df1$RR_mean)
Tony Ladson
  • 3,539
  • 1
  • 23
  • 30