首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发出ifelse()命令,其中两个不同数据帧中的两列看起来相同,但不标识为相同

发出ifelse()命令,其中两个不同数据帧中的两列看起来相同,但不标识为相同
EN

Stack Overflow用户
提问于 2021-04-12 22:00:03
回答 1查看 18关注 0票数 0

我在ifelse()命令中遇到了问题,其中两个不同数据帧中的两个列看起来是相同的,但它们并不是相同的。我可以使用任何指导来解决这个问题,这样代码就可以相互比较数据帧,并产生适当的输出,而不必自己手工输入/输入文本。

以下是我的两个起始数据集,df_1df_2

代码语言:javascript
复制
> 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

我想要创建来自df_2$Variable_analyses的列df_2$Variable_analyses,它基于df_1$DV_namedf_2$Variable_analyses之间的匹配材料。

下面是实现这一目标的,它是成功的:

代码语言:javascript
复制
> ## 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

我希望使用ifelse()命令更快地完成这项任务,并引用数据集,这就是我所称的快速方式。但是当我这样做的时候,它就不起作用了,产生了意想不到的结果。

我首先创建了一个变量,以消除列df_1$DV_namedf_2$Variable_analyses中的不可见字符。

代码语言:javascript
复制
### 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]"))

然后,我使用新变量df_1$DV_name_for_matchingdf_2$Variable_analyses_for_matching作为ifelse()命令的基础:

代码语言:javascript
复制
### 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)

这不会产生所需的输出(请参见上文)。相反,我得到了这个输出:

代码语言:javascript
复制
### displays df
# ---- NOTE: displays df, quick way does not work, not desired output
df_1

我不知道为什么快速方式不能工作。请告诉我如何使快速方式工作。

FYI,我在2013年英特尔Macbook Pro上使用RStudio。

谢谢。

下面是我用来创建帖子的代码

代码语言:javascript
复制
# 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-12 22:10:05

我相信你可以做一个left_join()

代码语言:javascript
复制
library(tidyverse)

left_join(df_1, df_2, by = c("DV_name" = "Variable_analyses"))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67066235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档