我有一個名為 Interest 的列,其中包含 10 個值。我需要使用 R 中的函式將“興趣”列中的值乘以 2,并在函式中使用新的乘積值創建一個新列。
感謝幫助,請。
我使用的公式為:
col<-function(col){
double<-col*2
return(double)
}
df_col_double
Col = Interest 列的名稱,并作為具有倍增值的新列加倍。
uj5u.com熱心網友回復:
您可以嘗試使用示例資料mtcars
col <- function(.data, col) {
.data |>
dplyr::mutate(dplyr::across({{col}}, ~.x*2, .names = "double_{col}"))
}
col(mtcars,col = carb)
uj5u.com熱心網友回復:
基礎 R 你可以使用:
doublecol <- function(data,col) {
data["new_col"]<-data[col]*2
return(data)
}
doublecol(iris,"Sepal.Length" )
uj5u.com熱心網友回復:
您不需要創建函式,可能您的問題已得到解答,但這里是使用 tidyverse 包并使用 mtcars 資料集中的 hp 列的方法:
library(tidyverse)
mtcars %>% mutate(. , hp_2=hp*2)
希望這就是您要找的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/536370.html
標籤:r
上一篇:為什么我的圖中省略了負值的冪?