R ifelse()命令出现问题,其中看起来相同的两个不同数据帧中的两列未标识为相同
ifelse()命令有一个问题,两个不同数据帧中看起来相同的两列没有被标识为相同。我可以使用任何指导来解决这个问题,这样代码就可以相互比较数据帧,并生成适当的输出,而不必亲自手动输入材料/键入文本 这是我的两个起始数据集,R ifelse()命令出现问题,其中看起来相同的两个不同数据帧中的两列未标识为相同,r,if-statement,encoding,match,R,If Statement,Encoding,Match,ifelse()命令有一个问题,两个不同数据帧中看起来相同的两列没有被标识为相同。我可以使用任何指导来解决这个问题,这样代码就可以相互比较数据帧,并生成适当的输出,而不必亲自手动输入材料/键入文本 这是我的两个起始数据集,df_1和df_2: > df_1 DV_name 1 submission_time_minutes 2 submission_time_minutes 3 WC 4
df_1
和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
我想根据df\u 1$DV\u name
和df\u 2$Variable\u analysis
之间的匹配材料,创建一列df\u 1$Variable\u label
,该列源自df\u 2$Variable\u analysis
### 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]"))
要做到这一点,还有很长的路要走,这是成功的:
> ## 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
然后,我使用新变量df\u 1$DV\u name\u进行匹配
和df\u 2$Variable\u analysiss\u进行匹配
作为ifelse()命令的基础:
我不知道为什么快速方式不起作用。请告知我如何使用快捷方式工作
仅供参考,我在2013 Intel Macbook Pro上使用RStudio
谢谢
下面是我用来创建帖子的代码
# 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
#创建df_1$Variable_标签
#----注意:带有要传输的值的列-df_2$Variable_标签
#----注:用于匹配的列-df_1$DV_名称,df_2$Variable_分析
##显示数据帧
df_1
df_2
##捷径
#----注意:快速方式不起作用
###创建匹配变量,从而从数据中删除一些不可见的字符
#----注意:对于df_1$DV_name,创建df_1$DV_name_进行匹配
df_1$DV_name_用于匹配我相信您只需执行左join()
我相信你可以做一个left\u join()
要比较的向量是两种不同的长度。您应该合并两个数据帧。如果您这样做,那么Variable\u Label
将重复。合并(df\u 1,df\u 2,by.x=“DV\u name”,by.y=“Variable\u analysis”,all.x=T)
在baseR
谢谢。我感谢这两个评论。另一位评论者的dplyr方法运行良好。我要用它,你们比较的向量是两种不同的长度。您应该合并两个数据帧。如果您这样做,那么Variable\u Label
将重复。合并(df\u 1,df\u 2,by.x=“DV\u name”,by.y=“Variable\u analysis”,all.x=T)
在baseR
谢谢。我感谢这两个评论。另一位评论者的dplyr方法运行良好。我要用这个,这个效果很好。谢谢这很有效。谢谢
### displays df
# ---- NOTE: displays df, quick way does not work, not desired output
df_1
# 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
library(tidyverse)
left_join(df_1, df_2, by = c("DV_name" = "Variable_analyses"))