简体   繁体   English

将列动态添加到 R 中的数据框

[英]Add columns dynamically to dataframe in R

I am only a few days old in the R ecosystem and trying to figure out a way to add dynamic column for each numeric column found in the original dataframe.我在 R 生态系统中只有几天大,并试图找出一种方法来为原始数据框中的每个数字列添加动态列。

I have succesfully written a way to change the value in the existing column in the dataframe but what I need is to put those calculated values into a new column rather than overwriting the existing one.我已经成功地编写了一种方法来更改数据框中现有列中的值,但我需要的是将这些计算值放入新列中,而不是覆盖现有列。

Here is what I've done so far,这是我到目前为止所做的,

myDf <- read.csv("MyData.csv",header = TRUE)

normalize <- function(x) {
    return ((x - min(x,na.rm = TRUE)) / (max(x,na.rm = TRUE) - min(x,na.rm = TRUE)))
}

normalizeAllCols <- function(df){
    df[,sapply(x, is.numeric)] <- lapply(df[,sapply(df, is.numeric)], normalize)
    df
}

normalizedDf<-normalizeAllCols(myDf)

I came with above snippet (with a lot of help from the internet) to apply normalize function to all numeric columns in the given data frame.我带着上面的代码片段(在互联网的很多帮助下)将 normalize 函数应用于给定数据框中的所有数字列。 I want to know how to put those calculated values into a new column in the data frame.我想知道如何将这些计算值放入数据框中的新列中。 (in the given snippet I'd like to know how to put normalized value in a new column like "norm" + colname ). (在给定的代码段中,我想知道如何将标准化值放在像"norm" + colname这样的新列中)。

You can find the column names which are numeric and use paste0 create new columns.您可以找到数字列名并使用paste0创建新列。

normalizeAllCols <- function(df){
  cols <- names(df)[sapply(df, is.numeric)]
  df[paste0('norm_', cols)] <- lapply(df[cols], normalize)
  df
}

normalizedDf<-normalizeAllCols(myDf)

In dplyr you can use across to apply a function to only numeric columns directly.dplyr你可以使用across到功能应用到仅直接数字列。

library(dplyr)
normalizeAllCols <- function(df){
 df %>%
    mutate(across(where(is.numeric), list(norm = ~normalize)))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM