我是 R 的新手。我希望撰寫一個函式來縮放資料框中除特定數字列之外的所有數字列(在下面的示例中,我不想縮放列“估計”)。由于使用此函式的特定背景關系,我實際上想使用另一個資料框縮放資料。下面是一個沒有奏效的嘗試。在本次嘗試中, original.df 表示需要縮放的資料框, scaling.data 表示用于縮放的資料。我試圖將數字 original.df 列放在相應的 scaling.data 列的平均值上,然后除以 scaling.data 列的 2 個標準差。
可能對作業解決方案不重要的其他資訊:
這個函式將嵌套在一個更大的函式中。在較大的函式中,有一個名為 predictors 的引數,它表示需要包含在新資料框中的列名稱,也可以在縮放資料框中找到。這可能是用于迭代縮放函式的向量,盡管這不一定是必需的。(注意:此向量包含參考字符和數字列的列名稱,我再次希望該函式僅縮放數字列。最終產品應包括來自 original.df 的未縮放的“估計”列)。
> predictors
[1] "color" "weight" "height" "length"
>original.df
color weight height length estimate
1 red 10 66 40 5
2 red 12 60 41 7
3 yellow 12 67 48 9
4 blue 15 55 36 10
5 yellow 21 54 48 7
6 red 12 54 43 5
7 red 11 38 36 6
>scale.data
color weight height length estimate
1 red 11 55 41 7
2 red 13 67 39 9
3 yellow 12 67 46 11
4 blue 16 8 37 5
5 yellow 23 10 47 9
6 red 17 11 41 10
7 red 16 13 37 13
scale2sd<-function(variable){
original.df[[variable]]<-((original.df[[variable]]) - mean(scaling.data[[variable]],na.rm=TRUE))/(2*sd(scaling.data[[variable]], na.rm=TRUE))
return(original.df[[variable]])
}
new.df<-original.df %>%mutate_at((!str_detect(names(.),"estimate")&is.numeric),scale)
我需要結果是完整的新縮放資料框。
非常感謝您的時間和想法。
uj5u.com熱心網友回復:
使用基本 R 的一種方式。代碼中的注釋。謝謝,尼爾森,提供資料 1
df <- read.table(text="color weight height length estimate
1 red 10 66 40 5
2 red 12 60 41 7
3 yellow 12 67 48 9
4 blue 15 55 36 10
5 yellow 21 54 48 7
6 red 12 54 43 5
7 red 11 38 36 6", head=T)
scale_df <- read.table(text=" color weight height length estimate
1 red 11 55 41 7
2 red 13 67 39 9
3 yellow 12 67 46 11
4 blue 16 8 37 5
5 yellow 23 10 47 9
6 red 17 11 41 10
7 red 16 13 37 13", head=T)
## add reference and scaling df as arguments
scale2sd <- function(ref, scale_by, variable) {
((ref[[variable]]) - mean(scale_by[[variable]], na.rm = TRUE)) / (2 * sd(scale_by[[variable]], na.rm = TRUE))
}
predictors <- c("color", "weight", "height", "length")
## this is to get all numeric columns that are part of your predictor variables
df_to_scale <- Filter(is.numeric, df[predictors])
## create a named vector. This is a bit awkward but it makes it easier to select
## the corresponding items in the two data frames,
## and then replace the original columns
num_vars <- setNames(names(df_to_scale), names(df_to_scale))
## this is the actual scaling job -
## use the named vector for looping over the selected columns
## then assign it back to the selected columns
df[num_vars] <- lapply(num_vars, function(x) scale2sd(df, scale_df, x))
df
#> color weight height length estimate
#> 1 red -0.67259271 0.58130793 -0.14222363 5
#> 2 red -0.42479540 0.47561558 -0.01777795 7
#> 3 yellow -0.42479540 0.59892332 0.85334176 9
#> 4 blue -0.05309942 0.38753862 -0.64000632 10
#> 5 yellow 0.69029252 0.36992323 0.85334176 7
#> 6 red -0.42479540 0.36992323 0.23111339 5
#> 7 red -0.54869405 0.08807696 -0.64000632 6
uj5u.com熱心網友回復:
我們可以執行以下操作(我使用的是dplyr1.0.7,但任何 >= 1.0.0 都可以):
創建一個可縮放的函式
scale_to_sd <- function(other_df, target){
mean(other_df[,target], na.rm=TRUE) /
(2*sd(other_df[, target], na.rm=TRUE))
}
如果你只需要嚴格的numeric列并且需要排除一些列,我們可以使用matches它比containseg提供更多的靈活性
df %>%
mutate(across(!matches("estimate|height") & where(is.numeric),
~ .x - scale_to_sd(scale_df,cur_column())))
以上將縮放任何東西,但估計或高度。可以玩轉正則運算式。
color weight height length estimate
1 red 8.088421 66 34.87995 5
2 red 10.088421 60 35.87995 7
3 yellow 10.088421 67 42.87995 9
4 blue 13.088421 55 30.87995 10
5 yellow 19.088421 54 42.87995 7
6 red 10.088421 54 37.87995 5
7 red 9.088421 38 30.87995 6
原來的
df %>%
mutate(across(contains("estimate") & where(is.numeric),
~ .x - scale_to_sd(scale_df,cur_column())))
跨目標列應用函式
df %>%
mutate(across(contains("estimate"),
~ .x - scale_to_sd(scale_df,cur_column())))
結果
color weight height length estimate
1 red 10 66 40 3.248164
2 red 12 60 41 5.248164
3 yellow 12 67 48 7.248164
4 blue 15 55 36 8.248164
5 yellow 21 54 48 5.248164
6 red 12 54 43 3.248164
7 red 11 38 36 4.248164
使用的資料:
df <- read.table(text="color weight height length estimate
1 red 10 66 40 5
2 red 12 60 41 7
3 yellow 12 67 48 9
4 blue 15 55 36 10
5 yellow 21 54 48 7
6 red 12 54 43 5
7 red 11 38 36 6", head=T)
scale_df <- read.table(text=" color weight height length estimate
1 red 11 55 41 7
2 red 13 67 39 9
3 yellow 12 67 46 11
4 blue 16 8 37 5
5 yellow 23 10 47 9
6 red 17 11 41 10
7 red 16 13 37 13", head=T)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/389565.html
上一篇:函式后的陣列
下一篇:正確使用地圖物件而不是引數
