0

I have two dataframes containing data from the same group of 32 subjects. Dataframe (B) has the results of a test ordered by subject number (1-32). I would like to move this data into Dataframe (A) where the rest of my data is and have subject data from (B) link to the corresponding data in (A). Both dataframes have a shared column named (SID) corresponding to the same person. I want to move the Score column from dataframe (B) to (A). How would I do this?

Have in (B):

SID 1, 2, 3, 4...
Score 27, 39, 19, 38...

Have in (A):

SID 1, 2, 3, 4...
Gender M, F, F, M...

Want in (A):

SID 1, 2, 3, 4...
Gender M, F, F, M...
Score 27, 39, 19, 38...

I really would like to have presented this in a better format, sorry about that!

  • 1
    It does, and in so much detail. I am very new to Rstudio, and so my very limited terminology when searching for possible answers in the previous questions search-bar is likely what is making it so that I don't find something which has been answered. Thank you for your link, there's a lot of information to digest. – Harry Caulton Aug 06 '20 at 17:13

2 Answers2

1

This is basically a merge task. As @NelsonGon suggested check that post and next time add data in order to reproduce your issue:

#Data
A <- data.frame(SID=1:4,Gender=c('M','F','F','M'),stringsAsFactors = F)
B <- data.frame(SID=1:4,Score=c(27,39,19,18))
#Merge
C <- merge(A,B,by = 'SID')

  SID Gender Score
1   1      M    27
2   2      F    39
3   3      F    19
4   4      M    18
Duck
  • 39,058
  • 13
  • 42
  • 84
  • I'll find out how to add data to a question before I post next time. I don't yet know how that's done. But thank you for your comment, solution, and recommendation :) – Harry Caulton Aug 06 '20 at 17:16
  • 1
    @HarryCaulton Great! Next time you can use `dput(yourdata)` or `dput(head(yourdata,20))` and paste the output in the question :) – Duck Aug 06 '20 at 17:17
1

You want some sort of join (https://dplyr.tidyverse.org/reference/join.html). I'll answer with dplyr::full_join:

## Create data frames
A = data.frame(
  SID   = c(1, 2, 3, 4),
  score = c(27, 39, 19, 38)
  )

B = data.frame(
  SID    = c(1, 2, 3, 4),
  gender = c("M", "F", "F", "M") 
  )

library(dplyr)

C = full_join(A, B, by = "SID")
C
#   SID score gender
# 1   1    27      M
# 2   2    39      F
# 3   3    19      F
# 4   4    38      M
richarddmorey
  • 976
  • 6
  • 19