我有这样的数据:
A1 A2 A3 A4 B C1 C2 C3 C4
1 3 2 2 7 2 NA 6 9
4 6 12 1 3 1 6 5 2
6 1 NA 1 7 3 2 2 1我想取以"C“开头的列的最大值,然后从"B”中减去以相同数字结尾的"A“列。例如,第一行中“C”的最大值为9,所以我想从B (7-2)中减去A4。
A1 A2 A3 A4 B C1 C2 C3 C4 new
1 11 2 2 7 2 NA 6 9 5
4 6 12 1 3 1 6 5 2 -3
6 1 NA 1 7 3 2 2 1 1这个是可能的吗?也许使用"starts_with"?
发布于 2022-08-29 20:40:55
下面是一个基本解决方案:
cs = grep("C", names(df))
as = grep("A", names(df))
c_max = apply(df[cs], 1, which.max)
df$new = df$B - as.matrix(df[as])[cbind(1:nrow(df), c_max)]
df
# A1 A2 A3 A4 B C1 C2 C3 C4 new
# 1 1 3 2 4 7 2 NA 6 9 3
# 2 4 6 12 1 3 1 6 5 2 -3
# 3 6 1 NA 1 7 3 2 2 1 1使用这些数据(注意,在您的输入中,输入中的A值与输出中的A值不同.我使用了输入。)
df = read.table(text = 'A1 A2 A3 A4 B C1 C2 C3 C4
1 3 2 4 7 2 NA 6 9
4 6 12 1 3 1 6 5 2
6 1 NA 1 7 3 2 2 1', header = T)发布于 2022-08-29 20:39:54
也许这能帮上忙:
library(dplyr)
df %>%
rowwise() %>%
mutate(idx = which.max(c_across(starts_with('C'))),
new = B - get(paste0('A', idx))) %>%
select(-idx)
# A tibble: 3 × 10
# Rowwise:
A1 A2 A3 A4 B C1 C2 C3 C4 new
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 3 2 4 7 2 NA 6 9 3
2 4 6 12 1 3 1 6 5 2 -3
3 6 1 NA 1 7 3 2 2 1 1发布于 2022-08-29 20:46:52
这里有一个带有max.col的
library(dplyr)
library(tidyr)
df1 %>%
mutate( new = B- as.data.frame(across(starts_with('A')))[
cbind(row_number(), max.col(across(starts_with('C'),
replace_na, -5), 'first'))])-output
A1 A2 A3 A4 B C1 C2 C3 C4 new
1 1 3 2 2 7 2 NA 6 9 5
2 4 6 12 1 3 1 6 5 2 -3
3 6 1 NA 1 7 3 2 2 1 1数据
df1 <- structure(list(A1 = c(1L, 4L, 6L), A2 = c(3L, 6L, 1L), A3 = c(2L,
12L, NA), A4 = c(2L, 1L, 1L), B = c(7L, 3L, 7L), C1 = c(2L, 1L,
3L), C2 = c(NA, 6L, 2L), C3 = c(6L, 5L, 2L), C4 = c(9L, 2L, 1L
)), class = "data.frame", row.names = c(NA, -3L))https://stackoverflow.com/questions/73534321
复制相似问题