如果一行中存在字符串,return 新列中存在该字符串的列 NAME
If string exists in a row, return the column NAME under which the string exists in a new column
我有一个像这样的数据框 text/descriptions:
Year Descr1 Descr2 Descr3
2017 Abby is cool Hi, call me 5 Don't call
1997 Is Doug cool? Is Gideon OK? Abby's coming at 5
2020 Hi i'm Gideon Hi i'm Abby OK, ttyl
My keywords are Doug and Abby. If any of my keywords appear in a row, I want a new column that tells me the column name. See below
``` FY Descr1 Descr2 Descr3 Found_in
2017 Abby is cool Hi call me Don't call Descr1
1997 Is Doug cool? Abby's coming Is Gideon OK? Descr1, Descr2
2020 Hi i'm Gideon OK, ttyl Hi i'm Abby Descr3 ```
right now I have:
df$Found_in <- ifelse(grepl(keywords,df[,1], ignore.case=TRUE), "Descr1",
ifelse(grepl(keywords,df[,2], ignore.case=TRUE), "Descr2",
ifelse(grepl(keywords,df[,3], ignore.case=TRUE), "Descr3", "NA"))) ```
但是我有一个非常大的数据框,所以我想知道是否有一种方法可以搜索我的关键字,如果它们存在于我的数据框中的任何地方,return 它们所在的列名称。
我希望这个问题是有道理的。也请原谅格式,这是我第一次在这里提问
这可以解决问题(我将使用您的第一个数据和解决方案来避免 ifelse
)但是必须对字符串进行一些考虑(例如,可以将它们转换得更低,这样更容易实现这种比较):
#Data
data <- structure(list(Year = c(2017, 1997, 2020), Descr1 = c("Abby is cool",
"Is Doug cool?", "Hi i'm Gideon"), Descr2 = c("Hi, call me 5",
"Is Gideon OK?", "Hi i'm Abby"), Descr3 = c("Don't call", "Abby's coming at 5",
"OK, ttyl")), row.names = c(NA, -3L), class = "data.frame")
#Code
data$Index <- apply(data[,-1],1,function(x) paste(names(x)[which(grepl(pattern = c('Doug|Abby'),x = x))],collapse=','))
它产生:
Year Descr1 Descr2 Descr3 Index
1 2017 Abby is cool Hi, call me 5 Don't call Descr1
2 1997 Is Doug cool? Is Gideon OK? Abby's coming at 5 Descr1,Descr3
3 2020 Hi i'm Gideon Hi i'm Abby OK, ttyl Descr2
我有一个像这样的数据框 text/descriptions:
Year Descr1 Descr2 Descr3
2017 Abby is cool Hi, call me 5 Don't call
1997 Is Doug cool? Is Gideon OK? Abby's coming at 5
2020 Hi i'm Gideon Hi i'm Abby OK, ttyl
My keywords are Doug and Abby. If any of my keywords appear in a row, I want a new column that tells me the column name. See below
``` FY Descr1 Descr2 Descr3 Found_in
2017 Abby is cool Hi call me Don't call Descr1
1997 Is Doug cool? Abby's coming Is Gideon OK? Descr1, Descr2
2020 Hi i'm Gideon OK, ttyl Hi i'm Abby Descr3 ```
right now I have:
df$Found_in <- ifelse(grepl(keywords,df[,1], ignore.case=TRUE), "Descr1",
ifelse(grepl(keywords,df[,2], ignore.case=TRUE), "Descr2",
ifelse(grepl(keywords,df[,3], ignore.case=TRUE), "Descr3", "NA"))) ```
但是我有一个非常大的数据框,所以我想知道是否有一种方法可以搜索我的关键字,如果它们存在于我的数据框中的任何地方,return 它们所在的列名称。
我希望这个问题是有道理的。也请原谅格式,这是我第一次在这里提问
这可以解决问题(我将使用您的第一个数据和解决方案来避免 ifelse
)但是必须对字符串进行一些考虑(例如,可以将它们转换得更低,这样更容易实现这种比较):
#Data
data <- structure(list(Year = c(2017, 1997, 2020), Descr1 = c("Abby is cool",
"Is Doug cool?", "Hi i'm Gideon"), Descr2 = c("Hi, call me 5",
"Is Gideon OK?", "Hi i'm Abby"), Descr3 = c("Don't call", "Abby's coming at 5",
"OK, ttyl")), row.names = c(NA, -3L), class = "data.frame")
#Code
data$Index <- apply(data[,-1],1,function(x) paste(names(x)[which(grepl(pattern = c('Doug|Abby'),x = x))],collapse=','))
它产生:
Year Descr1 Descr2 Descr3 Index
1 2017 Abby is cool Hi, call me 5 Don't call Descr1
2 1997 Is Doug cool? Is Gideon OK? Abby's coming at 5 Descr1,Descr3
3 2020 Hi i'm Gideon Hi i'm Abby OK, ttyl Descr2