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
  • 1
    Try this and let me know if it worked: `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 Jul 26 '20 at 17:54
  • It worked. Thank you so much! – btess Jul 26 '20 at 18:05
  • I have added that code as solution so that other people with similar issues can see. If you wish and feel comfortable with the answer you could accept it by clicking the tick on the left side of the answer :) – Duck Jul 26 '20 at 18:20
  • Awesome! Many thanks! – Duck Jul 26 '20 at 20:08

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
  • 39,058
  • 13
  • 42
  • 84
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
  • 351
  • 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 <- 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