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

mcvgt66p  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(101)

我正在尝试编写一个使用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")

我尝试过的事情:

-{{}}和!!运算符(Using function arguments as column names
-enquo

我得到的东西:

  • 列从输出中完全消失
  • 列名为questionID,而不是x214532
  • 该列名为questionID,所有文本都转换为x214532
    我肯定有一些地方我做错了,或者可能是我在pivot_longer中做对了一些事情,但还需要进一步修改语法,但我不能完全弄清楚。任何帮助都将不胜感激!
slmsl1lt

slmsl1lt1#

一个更简单的方法可能是使用tidyr::separate_longer_delim()而不是pivot_longer()和一个复杂的管道:

tidyr::separate_longer_delim(data = screening_data_responses_char, 
                             cols = 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

相关问题