R语言 使用变量名称创建新数据框

xnifntxz  于 2023-01-28  发布在  其他
关注(0)|答案(4)|浏览(186)

我有一个包含多个列的数据框,数据框的小型版本如下所示:

wind<- c(0.84, 1.77, 3.5, 6.44, 7.55) 
ROS<- c(0.01,0.03,0.05, 0.07, 0.1) 
T_0.1_1 <- c(1110, 350, 250, 300, 311)
T_0.2_1 <- c(560, 200, 364, 258, 159)
T_0.3_1 <- c(258, 147, 369, 123, 624)
T_180.1_1 <- c(554, 226, 547, 842, 366) 
T_180.2_1 <- c(258, 147, 369, 123, 624)
T_180.3_1 <- c(110, 350, 250, 300, 311)

df<-data.frame(wind,ROS,T_0.1_1,T_0.2_1,T_0.3_1, 
              T_180.1_1,T_180.2_1, T_180.3_1)

我想用变量“wind”和“ROS”创建另一个数据框,对于每个wind-ROS,我想选择以T_0和T_180开头的最大值变量,并使用该变量的名称创建一个新变量。新数据框应如下所示:

wind<- c(0.84, 1.77, 3.5, 6.44, 7.55) 
ROS<- c(0.01,0.03,0.05, 0.07, 0.1)
new_T0<- c('T_0.1_1', 'T_0.1_1', 'T_0.3_1', 'T_0.1_1', 'T_0.3_1')
new_T180<- c('T_180.1_1', 'T_180.3_1', 'T_180.1_1', 'T_180.1_1', 'T_180.2_1')

df_new<-data.frame(ROS, wind,new_T0, new_T180)

任何帮助我如何能做到这一点是非常感谢!谢谢!

ss2ws0br

ss2ws0br1#

df %>%
  pivot_longer(!1:2)%>%
  group_by(wind, ROS, id2=str_remove( name, '[.].*'))%>%
  slice_max(value)%>%
  pivot_wider(1:2, names_from = 'id2', values_from = 'name', names_prefix = 'new_')

# A tibble: 5 × 4
   wind   ROS new_T_0 new_T_180
  <dbl> <dbl> <chr>   <chr>    
1  0.84  0.01 T_0.1_1 T_180.1_1
2  1.77  0.03 T_0.1_1 T_180.3_1
3  3.5   0.05 T_0.3_1 T_180.1_1
4  6.44  0.07 T_0.1_1 T_180.1_1
5  7.55  0.1  T_0.3_1 T_180.2_1
pexxcrt2

pexxcrt22#

可能是个更好的方法但这能得到你想要的:

library(dplyr)
library(tidyr)

df2 <- df %>%
  tibble::rowid_to_column("id") %>%
  gather("key", "value",-c(id, wind, ROS)) %>%
  mutate(new_grp = ifelse(grepl("T_0", key), "new_T0", "New_T180")) %>%
  group_by(id, new_grp) %>%
  filter(value == max(value)) %>%
  ungroup() %>%
  select(id, wind, ROS, new_grp, key) %>%
  spread("new_grp", "key") %>%
  select(-id)
o4hqfura

o4hqfura3#

以下也是一种碱R溶液。

df<-data.frame(wind,ROS,T_0.1_1,T_0.2_1,T_0.3_1, 
               T_180.1_1,T_180.2_1, T_180.3_1)

new_df <- data.frame(wind, ROS)
new_df$new_T0 <- apply( df[, 3:8], MARGIN = 1, FUN = function(x) names(x)[which.max(x)])
new_df$new_T180 <- apply( df[, 3:8], MARGIN = 1, FUN = function(x) names(x)[which.min(x)])
jmo0nnb3

jmo0nnb34#

使用base R

cbind(df[1:2], lapply(split.default(df[-(1:2)],
  sub("\\..*", "", names(df)[-(1:2)])), \(x) names(x)[max.col(x, 'first')]))
  • 输出
wind  ROS     T_0     T_180
1 0.84 0.01 T_0.1_1 T_180.1_1
2 1.77 0.03 T_0.1_1 T_180.3_1
3 3.50 0.05 T_0.3_1 T_180.1_1
4 6.44 0.07 T_0.1_1 T_180.1_1
5 7.55 0.10 T_0.3_1 T_180.2_1

或使用tidyverse

library(dplyr)
library(purrr)
library(stringr)
df %>% 
   select(starts_with("T_")) %>% 
   split.default(str_remove(names(.), "\\..*")) %>% 
   map_dfc(~ names(.x)[max.col(.x, "first")]) %>%
   bind_cols(df %>%
     select(-starts_with("T_")), .)
  • 输出
wind  ROS     T_0     T_180
1 0.84 0.01 T_0.1_1 T_180.1_1
2 1.77 0.03 T_0.1_1 T_180.3_1
3 3.50 0.05 T_0.3_1 T_180.1_1
4 6.44 0.07 T_0.1_1 T_180.1_1
5 7.55 0.10 T_0.3_1 T_180.2_1

相关问题