Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

loops - 有没有一种方法可以dplyr(tidyverse)映射我的数据集,找到以相同后缀结尾的列,然后只保留一个?(Is there a way to dplyr (tidyverse) map my dataset, find columns ending with the same suffix then keep just one?)

Let's say I have two different measurement tools (a3 and a4) developed to assess psychological attributes, such as communication skills.

(假设我开发了两种不同的测量工具(a3和a4)来评估心理属性,例如沟通技巧。)

a3 tool is the first version of the instrument and it has 6 items (1,2,3,4,5,6).

(a3工具是该仪器的第一个版本,它有6个项目(1、2、3、4、5、6)。)

A4 is the updated version of a3 and it has only certain items developed to replace some items from the a3 version.

(A4是a3的更新版本,仅开发了某些项目来替代a3版本中的某些项目。)

Therefore, while my dataset always has items 1 to 6 from a3, I can have only items 1, 2 and 5 from a4 version.

(因此,虽然我的数据集始终具有a3中的项目1至6,但我只能具有a4版本中的项目1、2和5。)

As I have these two instruments, I labeled them by the domain_version_item.

(当我拥有这两种工具时,我用domain_version_item标记了它们。)

Therefore, I have a dataset like this one:

(因此,我有一个像这样的数据集:)

> asq_online %>% 
+   names
 [1] "id"           "age_interval" "com_a3_1"     "com_a3_2"     "com_a3_3"    
 [6] "com_a3_4"     "com_a3_5"     "com_a3_6"     "com_a4_1"     "com_a4_2"

If I want to return the a3 version, that's ok:

(如果我想返回a3版本,可以的:)

+   select(starts_with("com_a3")) %>% 
+   names
[1] "com_a3_1" "com_a3_2" "com_a3_3" "com_a3_4" "com_a3_5" "com_a3_6"
> 

If my goal is to return a4 only, that's ok:

(如果我的目标是仅返回a4,那就可以了:)

> asq_online %>% 
+   select(starts_with("com_a4")) %>% 
+   names
[1] "com_a4_1" "com_a4_2"

So, now I know the a4 version was made to replace the items 1 and 2 from a3 version and I should compose a dataset with com_a4_1, com_a4_2, com_a3_3, com_a3_4, com_a3_5, com_a3_6

(因此,现在我知道已经制作了a4版本来替换a3版本中的项目1和2,并且我应该使用com_a4_1,com_a4_2, com_a3_3,com_a3_4,com_a3_5,com_a3_6组成数据集)

I'm struggling to build this syntax.

(我正在努力建立这种语法。)

I see I have to compare the items with the same suffix and then replace it to have a print like that:

(我看到我必须将具有相同后缀的项目进行比较,然后将其替换以具有类似的打印效果:)

"com_a4_1" "com_a4_2" "com_a3_3" "com_a3_4" "com_a3_5" "com_a3_6"

(“ com_a4_1”“ com_a4_2”“ com_a3_3”“ com_a3_4”“ com_a3_5”“ com_a3_6”)

That's my dataset

(那是我的数据集)

asq_online <- structure(list(id = c(1, 2, 3, 4, 5), age_interval = c(12, 12, 
12, 12, 12), com_a3_1 = c(0, 0, 5, 0, 10), com_a3_2 = c(5, 0, 
10, 0, 5), com_a3_3 = c(10, 10, 10, 0, 5), com_a3_4 = c(5, 0, 
0, 10, 10), com_a3_5 = c(5, 5, 0, 10, 10), com_a3_6 = c(5, 10, 
0, 5, 5), com_a4_1 = c(10, 5, 5, 10, 10), com_a4_2 = c(10, 5, 
0, 0, 10)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", 
"data.frame"))

Please let me know if I'm missing any information.

(如果我缺少任何信息,请告诉我。)

I would like to remain within tidyverse environment.

(我想留在整洁的环境中。)

Thank you.

(谢谢。)

  ask by Luis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...