gpt4 book ai didi

r - R 代码中 pivot_longer 函数的问题

转载 作者:行者123 更新时间:2023-12-05 03:32:39 24 4
gpt4 key购买 nike

我在使用 datas 中的 pivot_longer 函数时遇到问题。你能帮我解决一下吗?

在这个问题中正常工作:How to adjust error when I have 0 values for graph generation .但是,在上一个问题中我没有使用 DTT 列,在当前问题中是。

library(dplyr)

df1 <- structure(
list(date1= c("2021-06-28","2021-06-28","2021-06-28"),
date2 = c("2021-06-30","2021-06-30","2021-07-02"),
DTT= c(NA,NA,"Hol"),
Week= c("Wednesday","Wednesday","Friday"),
Category = c("ABC","FDE","ABC"),
DR1 = c(4,1,0),
DR01 = c(4,1,0), DR02= c(4,2,0),DR03= c(9,5,0),
DR04 = c(5,4,0),DR05 = c(5,4,0)),
class = "data.frame", row.names = c(NA, -3L))


dmda<-"2021-07-02"
CategoryChosse<-"ABC"
DTest<-"Hol"

x<-df1 %>% select(starts_with("DR0"))

x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))
PV<-select(x, date2,Week, Category, DTT, DR1, ends_with("PV"))

med<-PV %>%
group_by(Category,Week,DTT) %>%
summarize(across(ends_with("PV"), median))

SPV<-df1%>%
inner_join(med, by = c('Category', 'Week','DTT')) %>%
mutate(across(matches("^DR0\\d+$"), ~.x +
get(paste0(cur_column(), '_PV')),
.names = '{col}_{col}_PV')) %>%
select(date1:Category, DR01_DR01_PV:last_col())

SPV<-data.frame(SPV)

mat1 <- df1 %>%
filter(date2 == dmda, Category == CategoryChosse, DTT==DTest) %>%
select(starts_with("DR0")) %>%
pivot_longer(cols = everything()) %>%
arrange(desc(row_number())) %>%
mutate(cs = cumsum(value)) %>%
filter(cs == 0) %>%
pull(name)

(dropnames <- paste0(mat1,"_",mat1, "_PV"))

SPV <- SPV %>%
filter(date2 == dmda, Category == CategoryChosse, DTT==DTest) %>%
select(-any_of(dropnames))

if(length(grep("DR0", names(SPV))) == 0) {
SPV[mat1] <- NA_real_
}

datas <-SPV %>%
filter(date2 == ymd(dmda)) %>%
group_by(Category, DTT) %>%
summarize(across(starts_with("DR0"), sum)) %>%
pivot_longer(cols= -Category, names_pattern = "DR0(.+)", values_to = "val") %>%
mutate(name = readr::parse_number(name))
colnames(datas)[-1]<-c("Days","Numbers")

Error: Can't combine `DTT` <character> and `DR05` <double>.
Run `rlang::last_error()` to see where the error occurred.

最佳答案

pivot_longer 检查列类型,并通过在 cols 中指定 -Category,它将选择所有剩余的列。但是,在 OP 的数据集中,除了其他 numeric 列(“DR0”)之外,还有一个 character 列“DTT”。一个选项是删除“DTT”(通过 %>% select(-DTT) %>% pivot_longer(..)并使用 OP 的代码或使用 cols = starts_with("DR0")

library(dplyr)
library(tidyr)
datas <- SPV %>%
filter(date2 == ymd(dmda)) %>%
group_by(Category, DTT) %>%
summarize(across(starts_with("DR0"), sum), .groups = "drop") %>%
pivot_longer(cols= starts_with("DR0"), names_pattern = "DR0(.+)",
values_to = "val") %>%
mutate(name = readr::parse_number(name))

-输出

> head(datas)
# A tibble: 5 × 4
Category DTT name val
<chr> <chr> <dbl> <dbl>
1 ABC Hol 5 NA
2 ABC Hol 4 NA
3 ABC Hol 3 NA
4 ABC Hol 2 NA
5 ABC Hol 1 NA

关于列名的变化,这里有4列。所以,我们可能需要

colnames(datas)[-c(1, 2)] <- c("Days","Numbers")

关于r - R 代码中 pivot_longer 函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70426954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com