![](/img/trans.png)
[英]R: For a list of dfs, filter for a value in Column1, to extract mean and SD of another Column2 (only of rows with the filtered value in Column1)
[英]Conditionally take value from column1 if the column1 name == first(value) from column2 BY GROUP
我有這個假的 dataframe:
df <- structure(list(Group = c(1L, 1L, 2L, 2L), A = 1:4, B = 5:8, C = 9:12,
X = c("A", "A", "B", "B")), class = "data.frame", row.names = c(NA, -4L))
Group A B C X
1 1 1 5 9 A
2 1 2 6 10 A
3 2 3 7 11 B
4 2 4 8 12 B
我嘗試改變一個新列,它應該采用在另一列中具有列名的列的值:
所需的 output:
Group A B C X new_col
1 1 5 9 A 1
1 2 6 10 A 1
2 3 7 11 B 7
2 4 8 12 B 7
到目前為止我的嘗試:
library(dplyr)
df %>%
group_by(Group) %>%
mutate(across(c(A,B,C), ~ifelse(first(X) %in% colnames(.), first(.), .), .names = "new_{.col}"))
Group A B C X new_A new_B new_C
<int> <int> <int> <int> <chr> <int> <int> <int>
1 1 1 5 9 A 1 5 9
2 1 2 6 10 A 1 5 9
3 2 3 7 11 B 3 7 11
4 2 4 8 12 B 3 7 11
一種選擇可能是:
df %>%
rowwise() %>%
mutate(new_col = get(X)) %>%
group_by(Group, X) %>%
mutate(new_col = first(new_col))
Group A B C X new_col
<int> <int> <int> <int> <chr> <int>
1 1 1 5 9 A 1
2 1 2 6 10 A 1
3 2 3 7 11 B 7
4 2 4 8 12 B 7
使用by
並將+ 1
添加到組號到 select 列。 假設組列按示例排列在"Group"
列之后。
transform(df, new_col=do.call(rbind, by(df, df$Group, \(x)
cbind(paste(x$X, x[1, x$Group[1] + 1])))))
# Group A B C X new_col
# 1 1 1 5 9 A A 1
# 2 1 2 6 10 A A 1
# 3 2 3 7 11 B B 7
# 4 2 4 8 12 B B 7
注意: R version 4.1.2 (2021-11-01)
。
數據:
df <- structure(list(Group = c(1L, 1L, 2L, 2L), A = 1:4, B = 5:8, C = 9:12,
X = c("A", "A", "B", "B")), class = "data.frame", row.names = c(NA,
-4L))
在base R
中,我們可以使用行/列索引
df$new_col <- df[2:4][cbind(match(unique(df$Group), df$Group)[df$Group],
match(df$X, names(df)[2:4]))]
df$new_col
[1] 1 1 7 7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.