我有一个数据框
structure(list(Brand = c("AB", "AC", "AD", "BA", "CB", "CK"), `&TV` = c("0_640",
"0", "1_340", "0", "0", "0"), `&TV HD` = c("1_500", "0", "0_140",
"0", "0", "0")), row.names = c(NA, 6L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
看起来像这样:
Brand &TV &TV HD
1 AB 0_640 1_500
2 AC 0 0
3 AD 1_340 0_140
4 BA 0 0
5 CB 0 0
6 CK 0 0
Run Code Online (Sandbox Code Playgroud)
我想返回其中包含模式的单元格的行和列值1_
。对于上面的例子,我想返回
row col
3 2
1 3
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 grep 但不是很成功。
grep(New_Brands_Theme_Combine[,c(1:length(New_Brands_Theme_Combine))], pattern = "1_")
Run Code Online (Sandbox Code Playgroud)
上面给出了找到模式的数据框的列号。如何获取列号和行号。
一种选择是循环遍历感兴趣的列('colnm'),应用grep
来获取位置索引,将与列索引一起设置并将names
其设置为两列 data.framelist
stack
colnm <- 2:3
out <- stack(setNames(lapply(df1[colnm], grep, pattern = "1_"), colnm))
names(out) <- c("row", "col")
out
# row col
#1 3 2
#2 1 3
Run Code Online (Sandbox Code Playgroud)