收集四列的值在两个键中

编程入门 行业动态 更新时间:2024-10-28 15:19:37
本文介绍了收集四列的值在两个键中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

有人问过类似的问题,但它们都是指将多个列收集到一个关键列中.

我需要两个键中的多个列.

这是我拥有的数据框:

<前>ID ... measure_A.1 measure_A.2 measure_B.1 measure_B.21 8.25 23.5 4 52 8.6 22.5 3 4

如果我使用以下代码,我会得到这个:

 df %>%收集(键=measure_A,值=score_A",measure_A.1,measure_A.2)%>%收集(键 = measure_B,值 = score_B",measure_B.1,measure_B.2)

<前>ID ...measure_A score_A measure_B score_B1 措施_A.1 8.25 措施_B.1 41 措施_A.1 8.25 措施_B.1 41 测量_A.2 23.5 测量_B.2 51 测量_A.2 23.5 测量_B.2 52 措施_A.1 8.6 措施_B.1 32 措施_A.1 8.6 措施_B.1 32 测量_A.2 22.5 测量_B.2 42 测量_A.2 22.5 测量_B.2 4

我想要的是这个:

<前>ID ... measure_A score_A measure_B score_B1 测量_A.1 8.25 测量_B.1 41 测量_A.2 23.5 测量_B.2 52 措施_A.1 8.6 措施_B.1 32 测量_A.2 22.5 测量_B.2 4

在我看来,我必须减少一些东西,但我不知道如何结合 gather 命令来做到这一点.我找到了一个带过滤器的解决方案,但我不明白它是如何工作的.

解决方案

这会以长格式提供所需的数据和所有值.

图书馆(tidyverse)df%>%pivot_longer(cols = -ID,names_to = c('.value', 'num'),names_sep = '\\.')# ID num measure_A measure_B# <int><chr><dbl><int>#1 1 1 8.25 4#2 1 2 23.5 5#3 2 1 8.6 3#4 2 2 22.5 4

之后我们需要做一些操作来获得准确的所需输出.

df %>%pivot_longer(cols = -ID,names_to = c('.value', 'num'),names_sep = '\\.') %>%rename_with(~sub('measure', 'score', .), starts_with('measure')) %>%变异(measure_A = str_c('MeasureA', num, sep = '.'),measure_B = str_c('MeasureB', num, sep = '.')) %>%选择(-数)# ID score_A score_B measure_A measure_B# <int><dbl><int><chr><chr>#1 1 8.25 4 MeasureA.1 MeasureB.1#2 1 23.5 5 MeasureA.2 MeasureB.2#3 2 8.6 3 MeasureA.1 MeasureB.1#4 2 22.5 4 MeasureA.2 MeasureB.2

Similar questions have been asked but they all refer to gathering multiple columns in one key column.

I need multiple columns in two keys.

This is the dataframe I have:

ID  ... measure_A.1 measure_A.2 measure_B.1 measure_B.2
1       8.25        23.5        4       5
2       8.6         22.5        3       4

If I use the following code I get this:

 df %>% 
 gather(key = measure_A, value = "score_A", measure_A.1, measure_A.2) %>%
 gather(key = measure_B, value = "score_B", measure_B.1, measure_B.2)

 
ID  ... measure_A   score_A     measure_B   score_B
1       measure_A.1 8.25        measure_B.1 4   
1       measure_A.1 8.25        measure_B.1 4
1       measure_A.2 23.5        measure_B.2 5
1       measure_A.2 23.5        measure_B.2 5
2       measure_A.1 8.6         measure_B.1 3
2       measure_A.1 8.6         measure_B.1 3
2       measure_A.2 22.5        measure_B.2 4
2       measure_A.2 22.5        measure_B.2 4

what I want is this:


ID  ... measure_A   score_A     measure_B   score_B
1       measure_A.1 8.25        measure_B.1 4   
1       measure_A.2 23.5        measure_B.2 5
2       measure_A.1 8.6         measure_B.1 3
2       measure_A.2 22.5        measure_B.2 4

It seems to me, that i have to reduce something but i don't know how to do this in combination with the gather command. I found a solution with filter, but I don't understand how that works.

解决方案

This gives the required data in long format with all the values.

library(tidyverse)

df %>%
  pivot_longer(cols = -ID, 
               names_to = c('.value', 'num'),
               names_sep = '\\.') 

#     ID num   measure_A measure_B
#  <int> <chr>     <dbl>     <int>
#1     1 1          8.25         4
#2     1 2         23.5          5
#3     2 1          8.6          3
#4     2 2         22.5          4

After that we need to do some manipulation to get the exact desired output.

df %>%
  pivot_longer(cols = -ID, 
               names_to = c('.value', 'num'),
               names_sep = '\\.') %>%
  rename_with(~sub('measure', 'score', .), starts_with('measure')) %>%
  mutate(measure_A = str_c('MeasureA', num, sep = '.'), 
         measure_B = str_c('MeasureB', num, sep = '.')) %>%
  select(-num)

#     ID score_A score_B measure_A  measure_B 
#  <int>   <dbl>   <int> <chr>      <chr>     
#1     1    8.25       4 MeasureA.1 MeasureB.1
#2     1   23.5        5 MeasureA.2 MeasureB.2
#3     2    8.6        3 MeasureA.1 MeasureB.1
#4     2   22.5        4 MeasureA.2 MeasureB.2

这篇关于收集四列的值在两个键中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 13:45:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395180.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!