引用函数参数作为pivot_longer中的列名

问题描述 投票:0回答:1

我正在尝试编写一个使用pivot_longer的函数,并且想使用我的函数对象作为pivot_longer中names_to参数的对象。

record <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
x214532 <- c("shirts, shoes",
"shoes, purses, hats",
"shirts, shoes, hats, heavy machinery",
"sponges, shoes",
"hats, heavy machinery",
"",
"heavy machinery, purses, shirts",
"heavy machinery, shoes, sponges",
"sponges",
"shoes")
screening_data_responses_char <- data.frame(record, x214532)
   record                              x214532
1       1                        shirts, shoes
2       2                  shoes, purses, hats
3       3 shirts, shoes, hats, heavy machinery
4       4                       sponges, shoes
5       5                hats, heavy machinery
6       6                                     
7       7      heavy machinery, purses, shirts
8       8      heavy machinery, shoes, sponges
9       9                              sponges
10     10                                shoes

最终我尝试取消连接列 x214532 并创建一个长数据集,将数据分成列中列出的项目,然后创建一个长数据集,如下所示:

   record         x214532
1       1          shirts
2       1           shoes
3       2           shoes
4       2          purses
5       2            hats
6       3          shirts
7       3           shoes
8       3            hats
9       3 heavy machinery
10      4         sponges
11      4           shoes
12      5            hats
13      5 heavy machinery
14      6                
15      7 heavy machinery
16      7          purses
17      7          shirts
18      8 heavy machinery
19      8           shoes
20      8         sponges
21      9         sponges
22     10           shoes

我希望包含数据的列仍被称为 x214532,但我在将其通过pivot_longer 的names_to 传递时遇到问题。这是我得到的:

remove_col_prefix <- function(x) {
  pattern <- "^[^_]+_"
  stringr::str_remove(x, pattern)
}

deconcatenate <- function(questionID) {
  screening_data_responses_questionID <- cSplit_e(data=screening_data_responses_char,split.col=questionID,sep=",",type="character")
  screening_data_responses_questionID <- screening_data_responses_questionID %>% 
    select(-questionID) %>% 
    pivot_longer(cols=c(starts_with(questionID)), 
                 names_to="questionID", 
                 values_to="questionID_resp") %>% 
    drop_na(questionID_resp) %>% 
    select(-questionID_resp) %>% 
    mutate(questionID=remove_col_prefix(questionID)) %>%
    select(c(deid_pat_id, questionID))
  
  screening_data_responses_char <- screening_data_responses_char %>% 
    select(-questionID)
  
  screening_data_responses_char <-merge(screening_data_responses_char,screening_data_responses_questionID,by="deid_pat_id",all=TRUE)
  }


screening_data_responses_char <- deconcatenate(questionID="x214532") 

我尝试过的事情:

-{{}} 和 !!运算符(使用函数参数作为列名称

-enquo

-这个水泥函数:(https://adv-r.hadley.nz/quasiquotation.html)

-稀疏(替换(x))

我得到的东西:

-该列从输出中完全消失

-该列称为 questionID 而不是 x214532

-该列称为 questionID,所有文本都变成 x214532

我确信我做错了一些事情,或者可能我在pivot_longer中做对了一些事情,但还需要进一步更改语法,但我不太明白。任何帮助将不胜感激!

r function concatenation tidyr data-cleaning
1个回答
0
投票

更简单的方法可能是使用

tidyr::separate_longer_delim()
:

tidyr::separate_longer_delim(screening_data_responses_char, x214532, delim = ",")

输出:

   record          x214532
1       1           shirts
2       1            shoes
3       2            shoes
4       2           purses
5       2             hats
6       3           shirts
7       3            shoes
8       3             hats
9       3  heavy machinery
10      4          sponges
11      4            shoes
12      5             hats
13      5  heavy machinery
14      6                 
15      7  heavy machinery
16      7           purses
17      7           shirts
18      8  heavy machinery
19      8            shoes
20      8          sponges
21      9          sponges
22     10            shoes
© www.soinside.com 2019 - 2024. All rights reserved.