整理数据:重命名列,获取非NA列名称,然后收集

时间:2019-03-17 05:46:12

标签: r dplyr tidyr stringr mutate

我要整理的数据非常难看,需要帮助!我的数据现在看起来像什么:

countries <- c("Austria", "Belgium", "Croatia")

df <- tibble("age" = c(28,42,19, 67),
         "1_recreate_1"=c(NA,15,NA,NA), 
         "1_recreate_2"=c(NA,10,NA,NA), 
         "1_recreate_3"=c(NA,8,NA,NA),
         "1_recreate_4"=c(NA,4,NA,NA),
         "1_fairness" = c(NA, 7, NA, NA),
         "1_confidence" = c(NA, 5, NA, NA),
         "2_recreate_1"=c(29,NA,NA,30),
         "2_recreate_2"=c(20,NA,NA,24),
         "2_recreate_3"=c(15,NA,NA,15),
         "2_recreate_4"=c(11,NA,NA,9),
         "2_fairness" = c(4, NA, NA, 1),
         "2_confidence" = c(5, NA, NA, 4),
         "3_recreate_1"=c(NA,NA,50,NA), 
         "3_recreate_2"=c(NA,NA,40,NA), 
         "3_recreate_3"=c(NA,NA,30,NA),
         "3_recreate_4"=c(NA,NA,20,NA),
         "3_fairness" = c(NA,  NA, 2, NA),
         "3_confidence" = c(NA, NA, 2, NA),
         "overall" = c(3,3,2,5))    

我需要它们在结尾处看起来像什么(硬编码):

df <- tibble(age = rep(c(28,42,19,67), each=4),
         country = rep(c("Belgium", "Austria", "Croatia", "Belgium"), each=4),
         recreate = rep(1:4, times=4),
         fairness = rep(c(4,7,2,1), each=4),
         confidence = rep(c(5,5,2,4), each=4),     
         allocation = c(29, 20, 15, 11,
                        15, 10, 8, 4,
                        50, 40, 30, 20, 
                        30, 24, 15, 9),
         overall = rep(c(3,3,2,5), each=4))

到达那里的步骤(我认为!):

1。使用我的国家/地区列表替换这些列的起始编号。
以字符串开头的数字是countries中的索引。换句话说,16_recreate_1将与向量countries中的第16个国家/地区对应。我认为以下代码可以工作(尽管不确定是否完全正确):

for(i in length(countries):1){
    colnames(df) <- str_replace(colnames(df), paste0(i,"_"), paste0(countries[i],"_"))
}  

2。通过获取每一行不是NA的列名,创建一个名为“ country”的新变量。

我尝试了which.maxnames的实验,但无法完全发挥作用。

3。创建新变量(recreate_1 ... recreate_4,以获取每一行的[country_name]_recreate_1 ... [country_name]_recreate_4值,无论该人所在国家/地区是否为不适用。< / strong>

也许rowSums是做到这一点的方法?

4。使数据变长而不是变宽 我认为这将需要gather,但是我不确定如何仅从变量countryrecreate_1 ... recreate_4进行收集。

很抱歉,这是如此复杂。最好使用Tidyverse解决方案,但非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

library(dplyr)
library(tidyr)
df %>% mutate(rid=row_number()) %>% 
       gather(key,val,-c(age,overall,rid, matches('recreate'))) %>% mutate(country=sub('(^\\d)_.*','\\1',key),country=countries[as.numeric(country)]) %>% 
       filter(!is.na(val)) %>% mutate(key=sub('(^\\d\\_)(.*)','\\2',key)) %>%
       spread(key,val) %>% gather(key = recreate,value = allocation,-c(rid,age,overall,Country,confidence,fairness)) %>% 
       filter(!is.na(allocation)) %>% mutate(recreate=sub('.*_(\\d$)','\\1',recreate))

这里(^\\d)_.*表示获得第一位数字,而.*_(\\d$)意味着获得最后一位数字。

答案 1 :(得分:1)

某种不同的tidyverse可能是:

df %>%
 gather(variable, allocation, na.rm = TRUE) %>%
 separate(variable, c("ID", "variable", "recreate"), convert = TRUE) %>%
 left_join(data.frame(countries) %>%
            mutate(country = countries,
                   ID = seq_along(countries)) %>%
            select(-countries), by = c("ID" = "ID")) %>%
 select(-variable, -ID) 

   recreate allocation country
      <int>      <dbl> <fct>  
 1        1         15 Austria
 2        2         10 Austria
 3        3          8 Austria
 4        4          4 Austria
 5        1         29 Belgium
 6        1         30 Belgium
 7        2         20 Belgium
 8        2         24 Belgium
 9        3         15 Belgium
10        3         15 Belgium
11        4         11 Belgium
12        4          9 Belgium
13        1         50 Croatia
14        2         40 Croatia
15        3         30 Croatia
16        4         20 Croatia

首先,将数据从宽格式转换为长格式,并使用NA删除行。其次,它将变量名称分为三列。第三,它将国家/地区的向量转换为df,并为每个国家/地区分配一个唯一的ID。最后,它将两者合并,并删除多余的变量。

已编辑问题的解决方案:

df %>%
 select(matches("(recreate)")) %>%
 rowid_to_column() %>%
 gather(var, allocation, -rowid, na.rm = TRUE) %>%
 separate(var, c("ID", "var", "recreate"), convert = TRUE) %>%
 select(-var) %>%
 left_join(data.frame(countries) %>%
            mutate(country = countries,
                   ID = seq_along(countries)) %>%
            select(-countries), by = c("ID" = "ID")) %>% 
 left_join(df %>%
            select(-matches("(recreate)")) %>%
            rowid_to_column() %>%
            gather(var, val, -rowid, na.rm = TRUE) %>%
            mutate(var = gsub("[^[:alpha:]]", "", var)) %>%
            spread(var, val), by = c("rowid" = "rowid")) %>%
 select(-rowid, -ID)

   recreate allocation country   age confidence fairness overall
      <int>      <dbl> <fct>   <dbl>      <dbl>    <dbl>   <dbl>
 1        1         15 Austria    42          5        7       3
 2        2         10 Austria    42          5        7       3
 3        3          8 Austria    42          5        7       3
 4        4          4 Austria    42          5        7       3
 5        1         29 Belgium    28          5        4       3
 6        1         30 Belgium    67          4        1       5
 7        2         20 Belgium    28          5        4       3
 8        2         24 Belgium    67          4        1       5
 9        3         15 Belgium    28          5        4       3
10        3         15 Belgium    67          4        1       5
11        4         11 Belgium    28          5        4       3
12        4          9 Belgium    67          4        1       5
13        1         50 Croatia    19          2        2       2
14        2         40 Croatia    19          2        2       2
15        3         30 Croatia    19          2        2       2
16        4         20 Croatia    19          2        2       2

在这里,首先,选择包含recreate的列,并添加具有行ID的列。其次,它遵循原始解决方案中的步骤。第三,它选择不包含recreate的列,执行从宽到长的数据转换,从列名中删除数字,然后将数据转换回原始的宽格式。最后,它将行上的两个ID合并在一起,并删除冗余变量。