Mel
Mel

Reputation: 510

Issue with ifelse() command, where 2 columns in 2 different data frames that look the same are not identified as identical

I'm having an issue with ifelse() command, where 2 columns in 2 different data frames that look the same are not identified as identical. I could use any guidance to fix this so that the code compares the data frames to each other and produces appropriate output instead of having to input material by hand/typing text myself.

Here are my 2 starting datasets, df_1 and df_2:

> df_1
                   DV_name
1  submission_time_minutes
2  submission_time_minutes
3                       WC
4                       WC
5         Analytic_z_score
6         Analytic_z_score
7            Clout_z_score
8            Clout_z_score
9        Authentic_z_score
10       Authentic_z_score
11            Tone_z_score
12            Tone_z_score
13 submission_time_minutes
14 submission_time_minutes
15                      WC
16                      WC
17        Analytic_z_score
18        Analytic_z_score
19           Clout_z_score
20           Clout_z_score
21       Authentic_z_score
22       Authentic_z_score
23            Tone_z_score
24            Tone_z_score
25 submission_time_minutes
26 submission_time_minutes
27                      WC
28                      WC
29        Analytic_z_score
30        Analytic_z_score
31           Clout_z_score
32           Clout_z_score
33       Authentic_z_score
34       Authentic_z_score
35            Tone_z_score
36            Tone_z_score
37 submission_time_minutes
38 submission_time_minutes
39                      WC
40                      WC
41        Analytic_z_score
42        Analytic_z_score
43           Clout_z_score
44           Clout_z_score
45       Authentic_z_score
46       Authentic_z_score
47            Tone_z_score
48            Tone_z_score
> df_2
        Variable_analyses             Variable_label
1 submission_time_minutes Submission time in minutes
2                      WC                 Word count
3        Analytic_z_score             Analytic score
4           Clout_z_score                Clout score
5       Authentic_z_score            Authentic score
6            Tone_z_score                 Tone score

I want to create the column df_1$Variable_label, derived from df_2$Variable_analyses, based on matching materials between df_1$DV_name and df_2$Variable_analyses.

Here is the long way to do this, which is successful:

> ## long way
> 
> ### creates Variable_label
> # ---- NOTE: does not directly extract Variable_label from df_2 and insert it into df_1
> # ---- NOTE: based on df_1$Variable_label
> df_1$Variable_label <- 
+   ifelse(df_1$DV_name == "submission_time_minutes", "Submission time in minutes",
+          ifelse(df_1$DV_name == "WC", "Word count",
+                 ifelse(df_1$DV_name == "Analytic_z_score", "Analytic score",
+                        ifelse(df_1$DV_name == "Clout_z_score", "Clout score",
+                               ifelse(df_1$DV_name == "Authentic_z_score", "Authentic score",
+                                      ifelse(df_1$DV_name == "Tone_z_score", "Tone score", NA
+                                      ))))))
> 
> ### displays df
> # ---- NOTE: displays df with created variable in desired output form
> df_1
                   DV_name             Variable_label
1  submission_time_minutes Submission time in minutes
2  submission_time_minutes Submission time in minutes
3                       WC                 Word count
4                       WC                 Word count
5         Analytic_z_score             Analytic score
6         Analytic_z_score             Analytic score
7            Clout_z_score                Clout score
8            Clout_z_score                Clout score
9        Authentic_z_score            Authentic score
10       Authentic_z_score            Authentic score
11            Tone_z_score                 Tone score
12            Tone_z_score                 Tone score
13 submission_time_minutes Submission time in minutes
14 submission_time_minutes Submission time in minutes
15                      WC                 Word count
16                      WC                 Word count
17        Analytic_z_score             Analytic score
18        Analytic_z_score             Analytic score
19           Clout_z_score                Clout score
20           Clout_z_score                Clout score
21       Authentic_z_score            Authentic score
22       Authentic_z_score            Authentic score
23            Tone_z_score                 Tone score
24            Tone_z_score                 Tone score
25 submission_time_minutes Submission time in minutes
26 submission_time_minutes Submission time in minutes
27                      WC                 Word count
28                      WC                 Word count
29        Analytic_z_score             Analytic score
30        Analytic_z_score             Analytic score
31           Clout_z_score                Clout score
32           Clout_z_score                Clout score
33       Authentic_z_score            Authentic score
34       Authentic_z_score            Authentic score
35            Tone_z_score                 Tone score
36            Tone_z_score                 Tone score
37 submission_time_minutes Submission time in minutes
38 submission_time_minutes Submission time in minutes
39                      WC                 Word count
40                      WC                 Word count
41        Analytic_z_score             Analytic score
42        Analytic_z_score             Analytic score
43           Clout_z_score                Clout score
44           Clout_z_score                Clout score
45       Authentic_z_score            Authentic score
46       Authentic_z_score            Authentic score
47            Tone_z_score                 Tone score
48            Tone_z_score                 Tone score

I want to use the ifelse() command to complete this task more quickly and reference the datasets, which is what I call the quick way. But when I do it, it does not work, producing undesired results.

I first created a variable to get rid of invisible characters in the columns df_1$DV_name and df_2$Variable_analyses.


### creates matching variables, which removes some invisible characters from data
# ---- NOTE: for df_1$DV_name, creating df_1$DV_name_for_matching
df_1$DV_name_for_matching <- 
  as.character(str_remove_all(df_1$DV_name, "[^A-z|0-9|[:punct:]|_|\\s]"))
# ---- NOTE: for df_2$Variable_analyses, creating 
df_2$Variable_analyses_for_matching <- 
  as.character(str_remove_all(df_2$Variable_analyses, "[^A-z|0-9|[:punct:]|_|\\s]"))

I then used the new variables df_1$DV_name_for_matching and df_2$Variable_analyses_for_matching as the basis for the ifelse() command:

### uses ifelse to complete matching task
df_1[["Variable_label"]] <- 
  ifelse(((df_1[["DV_name_for_matching"]]) == (df_2[["Variable_analyses_for_matching"]])), df_2[["Variable_label"]], NA)

This does not produce the desired output (please see above). Instead, I get this output:

### displays df
# ---- NOTE: displays df, quick way does not work, not desired output
df_1

I'm not sure why the quick way is not working. Please advise on how I can get the quick way to work.

FYI, I use RStudio on a 2013 Intel Macbook Pro.

Thanks.



Here is the code I used to create the post


# creates df_1$Variable_label
# ---- NOTE: column(s) with values to be transfered - df_2$Variable_label
# ---- NOTE: column(s) for matching - df_1$DV_name, df_2$Variable_analyses

## displays data frames
df_1
df_2

## quick way
# ---- NOTE: quick way does not work

### creates matching variables, which removes some invisible characters from data
# ---- NOTE: for df_1$DV_name, creating df_1$DV_name_for_matching
df_1$DV_name_for_matching <- 
  as.character(str_remove_all(df_1$DV_name, "[^A-z|0-9|[:punct:]|_|\\s]"))
# ---- NOTE: for df_2$Variable_analyses, creating 
df_2$Variable_analyses_for_matching <- 
  as.character(str_remove_all(df_2$Variable_analyses, "[^A-z|0-9|[:punct:]|_|\\s]"))

### uses ifelse to complete matching task
df_1[["Variable_label"]] <- 
  ifelse(((df_1[["DV_name_for_matching"]]) == (df_2[["Variable_analyses_for_matching"]])), df_2[["Variable_label"]], NA)

### displays df
# ---- NOTE: displays df, quick way does not work, not desired output
df_1


## long way

### creates Variable_label
# ---- NOTE: does not directly extract Variable_label from df_2 and insert it into df_1
# ---- NOTE: based on df_1$Variable_label
df_1$Variable_label <- 
  ifelse(df_1$DV_name == "submission_time_minutes", "Submission time in minutes",
         ifelse(df_1$DV_name == "WC", "Word count",
                ifelse(df_1$DV_name == "Analytic_z_score", "Analytic score",
                       ifelse(df_1$DV_name == "Clout_z_score", "Clout score",
                              ifelse(df_1$DV_name == "Authentic_z_score", "Authentic score",
                                     ifelse(df_1$DV_name == "Tone_z_score", "Tone score", NA
                                     ))))))

### displays df
# ---- NOTE: displays df with created variable in desired output form
df_1

Upvotes: 0

Views: 20

Answers (1)

tonybot
tonybot

Reputation: 655

I believe you can just do a left_join().

library(tidyverse)

left_join(df_1, df_2, by = c("DV_name" = "Variable_analyses"))

Upvotes: 2

Related Questions