0

我试图在几列中逐行获取最大值,并使用最大值和相应的列名创建 2 个新列。然后,使用列名,我需要选择另一个列的值,该列共享该列名的子字符串。

这是我试图解决的一个例子:

measure_day1 <-  c(1,2,5)
measure_day2 <- c(5,7,1)
measure_day3 <- c(2,3,9)
temp_day1 <- c(25, 27, 29)
temp_day2 <- c(31, 33, 35)
temp_day3 <- c(14, 16, 19)

df <- data.frame(measure_day1, measure_day2, measure_day3, temp_day1, temp_day2, temp_day3)

  measure_day1 measure_day2 measure_day3 temp_day1 temp_day2 temp_day3
1            1            5            2        25        31        14
2            2            7            3        27        33        16
3            5            1            9        29        35        19

这将是结果:

 measure_day1 measure_day2 measure_day3 temp_day1 temp_day2 temp_day3 measure_max day_measure_max temp_day_measure_max
1            1            5            2        25        31        14           5    measure_day2                   31
2            2            7            3        27        33        16           7    measure_day2                   33
3            5            1            9        29        35        19           9    measure_day3                   19

我发现了这个类似的问题,但无法完成我的任务。

对于每一行,返回最大值的列名

任何帮助是极大的赞赏。

4

2 回答 2

1

试试这个tidyverse方法。对数据进行重塑可能更实用,因为之前很早就为每行创建一个 id,然后使用filter. 使用pivot_wider()您可以获得所需的值,然后应用最大值过滤器。left_join()最后,您可以使用基于行创建的 id合并到原始数据。这里的代码:

library(dplyr)
library(tidyr)
#Code
newdf <- df %>% mutate(id=1:n()) %>%
  left_join(df %>% mutate(id=1:n()) %>%
  pivot_longer(-id) %>%
  separate(name,c('Var','Day'),sep='_') %>%
  pivot_wider(names_from=Var,values_from=value) %>%
  group_by(id) %>%
  filter(measure==max(measure)) %>%
  mutate(Day=paste0('measure_',Day)) %>% select(-measure) %>%
  rename(measure_max=Day,temp_day_measure_max=temp)) %>% select(-id)

输出:

  measure_day1 measure_day2 measure_day3 temp_day1 temp_day2 temp_day3  measure_max temp_day_measure_max
1            1            5            2        25        31        14 measure_day2                   31
2            2            7            3        27        33        16 measure_day2                   33
3            5            1            9        29        35        19 measure_day3                   19
于 2020-10-21T14:28:20.887 回答
1

基础 R 方法:

#Select measure columns
measure_cols <- grep('measure', names(df), value = TRUE)
#Select temp_day column
temp_cols <- grep('temp_day', names(df))
#Get the index of max value in measure column
inds <- max.col(df[measure_cols])
#Get max value in measure columns
df$measure_max <- do.call(pmax, df[measure_cols])
#Get the name of max value in measure columns
df$day_measure_max <- measure_cols[inds]
#Get the corresponding temp column. 
df$temp_day_measure_max <- df[temp_cols][cbind(1:nrow(df), inds)]
df

#  measure_day1 measure_day2 measure_day3 temp_day1 temp_day2 temp_day3
#1            1            5            2        25        31        14
#2            2            7            3        27        33        16
#3            5            1            9        29        35        19

#  measure_max day_measure_max temp_day_measure_max
#1           5    measure_day2                   31
#2           7    measure_day2                   33
#3           9    measure_day3                   19
于 2020-10-21T14:30:53.803 回答