使用函数内的列名跨列应用函数
apply function across columns using column names within function
我正在尝试迭代 100 多个列以确定单独列中的变量是否与列名匹配。我想也许跨函数可能能够但无法弄清楚如何在每一列上使用 mutate。
请参阅下面的示例。
tst=structure(list(type = c("DOG", "DOG", "DOG", "CAT", "CAT", "CAT",
"MOUSE", "MOUSE", "MOUSE"), CAT = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), DOG = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), MOUSE = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), id = 1:9), row.names = c(NA, -9L
), class = c("tbl_df", "tbl", "data.frame"))
我的 table 当前具有以下结构。
type CAT DOG MOUSE id
<chr> <chr> <chr> <chr> <int>
1 DOG NA NA NA 1
2 DOG NA NA NA 2
3 DOG NA NA NA 3
4 CAT NA NA NA 4
5 CAT NA NA NA 5
6 CAT NA NA NA 6
7 MOUSE NA NA NA 7
8 MOUSE NA NA NA 8
9 MOUSE NA NA NA 9
我希望最终结果如下所示:
type CAT DOG MOUSE id
<chr> <chr> <chr> <chr> <int>
1 DOG NA TRUE NA 1
2 DOG NA TRUE NA 2
3 DOG NA TRUE NA 3
4 CAT TRUE NA NA 4
5 CAT TRUE NA NA 5
6 CAT TRUE NA NA 6
7 MOUSE NA NA TRUE 7
8 MOUSE NA NA TRUE 8
9 MOUSE NA NA TRUE 9
This works but it is not sufficient for 100 columns.
tst<-tst%>%mutate(CAT=ifelse(type==names(tst[2]),'TRUE',NA))
tst<-tst%>%mutate(DOG=ifelse(type==names(tst[3]),'TRUE',NA))
tst<-tst%>%mutate(MOUSE=ifelse(type==names(tst[4]),'TRUE',NA))
候选方案如下(无dplyr
)
# initialise list
tmpList <- list()
# iterate over each row
for (i in 1:nrow(tst)) {
tmpList[[i]] <- colnames(tst[-c(1,5)]) %in% tst$type[i]
}
# save as data frame
output <- as.data.frame(do.call(rbind, tmpList))
colnames(output) <- colnames(tst[-c(1,5)])
# cbind with data
output <- cbind(tst[,c(1,5)],output)
这给了你正在寻找的东西!如果有更好的解决方案,我不是很容易想到。
最好!
dplyr
对此的解决方案非常巧妙。您可以使用 across()
和 cur_column()
来获取当前列的名称,并将其放入您的公式中:
tst %>%
mutate(across(CAT:MOUSE, ~if_else(type == cur_column(), 'TRUE', .x)))
您可以将 CAT:MOUSE
替换为您需要的任何整洁的 select 函数来获取 100 多列。
这是完整的代表:
library(tidyverse)
# I like tibbles because they print nicely, but this could just be a plain dataframe
tst <- tibble(
type = c(
"DOG", "DOG", "DOG", "CAT", "CAT", "CAT",
"MOUSE", "MOUSE", "MOUSE"
),
CAT = NA_character_,
DOG = NA_character_,
MOUSE = NA_character_,
id = 1:9
)
# .x here could be NA_character_, if you don't want the value from the existing column
tst %>%
mutate(across(CAT:MOUSE, ~if_else(type == cur_column(), 'TRUE', .x)))
#> # A tibble: 9 x 5
#> type CAT DOG MOUSE id
#> <chr> <chr> <chr> <chr> <int>
#> 1 DOG <NA> TRUE <NA> 1
#> 2 DOG <NA> TRUE <NA> 2
#> 3 DOG <NA> TRUE <NA> 3
#> 4 CAT TRUE <NA> <NA> 4
#> 5 CAT TRUE <NA> <NA> 5
#> 6 CAT TRUE <NA> <NA> 6
#> 7 MOUSE <NA> <NA> TRUE 7
#> 8 MOUSE <NA> <NA> TRUE 8
#> 9 MOUSE <NA> <NA> TRUE 9
由 reprex package (v1.0.0)
于 2021-04-23 创建
我正在尝试迭代 100 多个列以确定单独列中的变量是否与列名匹配。我想也许跨函数可能能够但无法弄清楚如何在每一列上使用 mutate。 请参阅下面的示例。
tst=structure(list(type = c("DOG", "DOG", "DOG", "CAT", "CAT", "CAT",
"MOUSE", "MOUSE", "MOUSE"), CAT = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), DOG = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), MOUSE = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), id = 1:9), row.names = c(NA, -9L
), class = c("tbl_df", "tbl", "data.frame"))
我的 table 当前具有以下结构。
type CAT DOG MOUSE id
<chr> <chr> <chr> <chr> <int>
1 DOG NA NA NA 1
2 DOG NA NA NA 2
3 DOG NA NA NA 3
4 CAT NA NA NA 4
5 CAT NA NA NA 5
6 CAT NA NA NA 6
7 MOUSE NA NA NA 7
8 MOUSE NA NA NA 8
9 MOUSE NA NA NA 9
我希望最终结果如下所示:
type CAT DOG MOUSE id
<chr> <chr> <chr> <chr> <int>
1 DOG NA TRUE NA 1
2 DOG NA TRUE NA 2
3 DOG NA TRUE NA 3
4 CAT TRUE NA NA 4
5 CAT TRUE NA NA 5
6 CAT TRUE NA NA 6
7 MOUSE NA NA TRUE 7
8 MOUSE NA NA TRUE 8
9 MOUSE NA NA TRUE 9
This works but it is not sufficient for 100 columns.
tst<-tst%>%mutate(CAT=ifelse(type==names(tst[2]),'TRUE',NA))
tst<-tst%>%mutate(DOG=ifelse(type==names(tst[3]),'TRUE',NA))
tst<-tst%>%mutate(MOUSE=ifelse(type==names(tst[4]),'TRUE',NA))
候选方案如下(无dplyr
)
# initialise list
tmpList <- list()
# iterate over each row
for (i in 1:nrow(tst)) {
tmpList[[i]] <- colnames(tst[-c(1,5)]) %in% tst$type[i]
}
# save as data frame
output <- as.data.frame(do.call(rbind, tmpList))
colnames(output) <- colnames(tst[-c(1,5)])
# cbind with data
output <- cbind(tst[,c(1,5)],output)
这给了你正在寻找的东西!如果有更好的解决方案,我不是很容易想到。
最好!
dplyr
对此的解决方案非常巧妙。您可以使用 across()
和 cur_column()
来获取当前列的名称,并将其放入您的公式中:
tst %>%
mutate(across(CAT:MOUSE, ~if_else(type == cur_column(), 'TRUE', .x)))
您可以将 CAT:MOUSE
替换为您需要的任何整洁的 select 函数来获取 100 多列。
这是完整的代表:
library(tidyverse)
# I like tibbles because they print nicely, but this could just be a plain dataframe
tst <- tibble(
type = c(
"DOG", "DOG", "DOG", "CAT", "CAT", "CAT",
"MOUSE", "MOUSE", "MOUSE"
),
CAT = NA_character_,
DOG = NA_character_,
MOUSE = NA_character_,
id = 1:9
)
# .x here could be NA_character_, if you don't want the value from the existing column
tst %>%
mutate(across(CAT:MOUSE, ~if_else(type == cur_column(), 'TRUE', .x)))
#> # A tibble: 9 x 5
#> type CAT DOG MOUSE id
#> <chr> <chr> <chr> <chr> <int>
#> 1 DOG <NA> TRUE <NA> 1
#> 2 DOG <NA> TRUE <NA> 2
#> 3 DOG <NA> TRUE <NA> 3
#> 4 CAT TRUE <NA> <NA> 4
#> 5 CAT TRUE <NA> <NA> 5
#> 6 CAT TRUE <NA> <NA> 6
#> 7 MOUSE <NA> <NA> TRUE 7
#> 8 MOUSE <NA> <NA> TRUE 8
#> 9 MOUSE <NA> <NA> TRUE 9
由 reprex package (v1.0.0)
于 2021-04-23 创建