我想根據列名中的常見內容將一組列除以另一組列。更具體地說,在下文中,dataframe我想將帶有 prefix1 的每列與帶有 prefix2 的相應列分開(即“prefix1 column1”與“prefix2 column1”,“prefix1 column2”與“prefix2 column2”等)。

dt <- data.frame(replicate(6,sample(1:15,10,rep=TRUE)))
colnames(dt) <- c("prefix1 column1","prefix1 column2","prefix1 column3","prefix2 column1","prefix2 column2","prefix2 column3")
View(dt)
理想的輸出將是一個帶有附加 3 列的資料框,其中包含除法結果。我的頭腦嚴重堅持這項任務 - 我將不勝感激任何建議。
uj5u.com熱心網友回復:
我們可以回圈across'prefix1' 列,將列名 ( cur_column()) 中的子字串 'prefix1' 替換為'prefix2',get該值和除法,通過更新.names
library(dplyr)
library(stringr)
dt <- dt %>%
mutate(across(starts_with('prefix1'), ~ ./get(str_replace(cur_column(),
'prefix1', 'prefix2')), .names = '{.col}_new'))
或使用 base R
dt[paste0(names(dt)[1:3], "_new")] <- dt[1:3]/dt[4:6]
uj5u.com熱心網友回復:
另一種解決方案,基于purrr::reduce:
library(tidyverse)
dt <- data.frame(replicate(6,sample(1:15,10,rep=TRUE)))
colnames(dt) <- c("prefix1 column1","prefix1 column2","prefix1 column3","prefix2 column1","prefix2 column2","prefix2 column3")
reduce(paste0("column",1:3), function(x,y)
{ z <- x[,paste("prefix1",y)] / x[,paste("prefix2",y)];
bind_cols(x, z %>% data.frame %>% setNames(. ,paste0("d",y))) }, .init=dt)
#> prefix1 column1 prefix1 column2 prefix1 column3 prefix2 column1
#> 1 2 12 4 13
#> 2 13 14 12 11
#> 3 6 3 4 6
#> 4 1 9 5 6
#> 5 15 1 2 8
#> 6 4 7 15 1
#> 7 5 9 1 10
#> 8 4 10 1 5
#> 9 6 8 3 12
#> 10 13 13 9 2
#> prefix2 column2 prefix2 column3 dcolumn1 dcolumn2 dcolumn3
#> 1 15 15 0.1538462 0.80000000 0.2666667
#> 2 1 11 1.1818182 14.00000000 1.0909091
#> 3 5 1 1.0000000 0.60000000 4.0000000
#> 4 13 13 0.1666667 0.69230769 0.3846154
#> 5 15 4 1.8750000 0.06666667 0.5000000
#> 6 9 4 4.0000000 0.77777778 3.7500000
#> 7 7 8 0.5000000 1.28571429 0.1250000
#> 8 14 5 0.8000000 0.71428571 0.2000000
#> 9 13 1 0.5000000 0.61538462 3.0000000
#> 10 14 14 6.5000000 0.92857143 0.6428571
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/347584.html
下一篇:找出與回應最相關的5個變數
