0

I need help with working with two different datasets for my research project.

I have two different data frames, they have different number of columns and rows. I need to gather values from one dataset that satisfy a specific condition that involves both datasets. The condition to be satisfied: that the combination of two values (in the same row but different columns) are the same.

For example, in my dataset, the values in 'data$Regular_response' should be the same as 'swow$response', and data$Test_word should be the same as swow$cue.

data$Regular_response = swow$response data$Test_word = swow$cue

In other words, I am looking for equal word pairs in both datasets.

When this condition is satisfied, I need the value of swow$R123.Strength to be printed in a new column in data$strength

How do I do that??

> head(swow)
# A tibble: 6 x 5
  cue   response R123  N     R123.Strength     
  <chr> <chr>    <chr> <chr> <chr>             
1 a     one      31    257   0.120622568093385 
2 a     the      26    257   0.101167315175097 
3 a     an       17    257   0.066147859922179 
4 a     b        14    257   0.0544747081712062
5 a     single   9     257   0.0350194552529183
6 a     article  6     257   0.0233463035019455
> head(data)
   Regular_response Test_word Pyramids_and_Palms_Test
1:         princess     queen                      92
2:            shoes  slippers                      92
3:        flowerpot      vase                      92
4:            horse     zebra                      92
5:              cup      bowl                      85
6:              nun    church                      85
> filter(data, Test_word == 'queen', Regular_response == 'princess')
  Regular_response Test_word Pyramids_and_Palms_Test
1         princess     queen                      92
2         princess     queen                      87
> filter(swow, cue == 'queen', response == 'princess')
# A tibble: 1 x 5
  cue   response R123  N     R123.Strength     
  <chr> <chr>    <chr> <chr> <chr>             
1 queen princess 3     292   0.0102739726027397

I appreciate those who can help me with this code!

btess
  • 23
  • 4

2 Answers2

1

Try this solution as I told you earlier:

Merged <- merge(data,swow[,c("cue","response","R123.Strength")],by.x = c('Test_word','Regular_response'),by.y=c('cue','response'),all.x=T)
Duck
  • 38,442
  • 13
  • 38
  • 79
0

Sounds like a job for a join. So something like:

data <- data %>% 
   left_join(swow, by = c("Regular_response" = "response", "Test_word" = "cue")) %>%
   mutate(strength = R123.Strength)
eugene100hickey
  • 341
  • 1
  • 3
  • Hi, thank you. When I do that, this error comes up: ```Error in mutate(strength = R123.Strength) : object 'R123.Strength' not found```do you know how to solve that? Thanks!! – btess Jul 26 '20 at 17:58
  • Sorry, I missed a parenthesis in the response above. Should be: data % left_join(swow, by = c("Regular_response" = "response", "Test_word" = "cue")) %>% mutate(strength = R123.Strength) Note the exxtra parenthesis after "cue". – eugene100hickey Jul 27 '20 at 19:19