gpt4 book ai didi

r - 按行识别匹配项,并使用匹配项的列名创建列

转载 作者:行者123 更新时间:2023-12-05 04:21:05 26 4
gpt4 key购买 nike

我一直在尝试(未成功)使用来自多个线程的解决方案,尤其是: Get column name based on row values matching in DF in R (more than 3 column names) Populate a new column if a value is found in any column但是当我使它们适应我的特定需求时,我失败了……其他帖子处理与特定值或最大值等的匹配,但这对我不起作用。

所以我有一个变量(日期),我需要根据其他日期变量知道该日期的来源。我有一个非常大的数据集,所以我需要保留其余部分,并避免使用对列号的引用(例如 df[2:3]),因为我的数据源将来可能会发生变化,所以最好坚持到变量名称(例如,“date1”)。最好举例说明:

df<-data.frame(id=as.character(c(1,2,3,4,5)), 
date1=c("01/10/2022",NA,"1/10/2022","2/10/2022",NA),
date2=c("02/10/2022",NA,"3/10/2022","4/10/2022","02/10/2022"),
date3=c("05/10/2022","3/10/2022", "5/10/2022","6/10/2022","04/10/2022"),
keydate=c("01/10/2022","3/10/2022", "1/10/2022","4/10/2022","05/10/2022"))

df$date1 <- as.Date(df$date1, format="%d/%m/%y")
df$date2 <- as.Date(df$date2, format="%d/%m/%y")
df$date3 <- as.Date(df$date3, format="%d/%m/%y")
df$keydate <- as.Date(df$keydate, format="%d/%m/%y")

然后我需要一个新变量“datesource”来定义“keydate”的来源,在我之前的例子中是这样的:

datesource=c("date1","date3","date1","date2","none")
cbind(df,datesource)

我的一些失败尝试包括:

df$datesource = ifelse(df$keydate %in% df[c(date1,date2,date3),], colnames(df), "none") #get all "none"
#OR
df %>% mutate(datesource = ifelse(df$keydate %in% df, colnames(df), "none")) #get all "none"
#OR
df$datesource <- apply(df, colnames(df[c(date1,date2,date3),]),
function(x) ifelse(any(x = df$keydate), colnames(df), 'none'))
#Error in apply(df, colnames(df[c(date1, date2, date3), ]), function(x) ifelse(any(x = df$keydate), : 'X' must have named dimnames

谢谢你的帮助!

最佳答案

这是一个选项。您可以在列名向量中映射出正确的位置:

library(tidyverse)

df |>
mutate(datesource = pmap_chr(list(date1, date2, date3, keydate),
~c("date1", "date2", "date3")[
c(..1, ..2, ..3) %in% ..4
]))
#> id date1 date2 date3 keydate datesource
#> 1 1 2020-10-01 2020-10-02 2020-10-05 2020-10-01 date1
#> 2 2 <NA> <NA> 2020-10-03 2020-10-03 date3
#> 3 3 2020-10-01 2020-10-03 2020-10-05 2020-10-01 date1
#> 4 4 2020-10-02 2020-10-04 2020-10-06 2020-10-04 date2
#> 5 5 <NA> 2020-10-02 2020-10-04 2020-10-02 date2

编辑包括“无”的选项

library(tidyverse)

df |>
mutate(datesource = pmap_chr(list(date1, date2, date3, keydate),
~ifelse(any(c(..1, ..2, ..3) %in% ..4),
c("date1", "date2", "date3")[c(..1, ..2, ..3) %in% ..4],
"none")))
#> id date1 date2 date3 keydate datesource
#> 1 1 2020-10-01 2020-10-02 2020-10-05 2020-10-01 date1
#> 2 2 <NA> <NA> 2020-10-03 2020-10-03 date3
#> 3 3 2020-10-01 2020-10-03 2020-10-05 2020-10-01 date1
#> 4 4 2020-10-02 2020-10-04 2020-10-06 2020-10-04 date2
#> 5 5 <NA> 2020-10-02 2020-10-04 2020-10-05 none

关于r - 按行识别匹配项,并使用匹配项的列名创建列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74316965/

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