【问题标题】:R: create a function that operates on different rows and creates a new column with its valueR:创建一个对不同行进行操作并使用其值创建一个新列的函数
【发布时间】:2020-12-28 12:43:31
【问题描述】:

我有以下数据框(df1)。此数据框包含一个名为“平均值”的行,其中包含每列的平均值。

         GDP    per_capita 
France    2         5
Spain     4         10
Italy     6         15
Mean      4         10

我想创建一个复制 df1 列的函数,每个新列的值是每个单元格的减法减去其各自的平均值,再除以它的平均值。像这样:

         GDP    per_capita   GDP_diff   per_capita_diff
France    2         5        (2-4)/4      (5-10)/10
Spain     4         10       (4-4)/4      (10-10)/10
Italy     6         15       (6-4)/4      (15-10)/10
Mean      4         10       (4-4)/4      (10-10)/10

所以最后应该是这样的:

       GDP    per_capita    GDP_diff   per_capita_diff
France   2        5         -0.5          -0.5
Spain    4        10            0           0
Italy    6        15         0.5           0.5
Mean     4        10           0            0

我必须假设将使用此函数的每个数据帧都有一个名为“平均值”的行。 到目前为止,这就是我所拥有的:

new.function <- function(df){
  name.df= colnames(df)
  new.df = apply(df, FUN = function(x) (x-Mean)/Mean, MARGIN = 2)
  colnames(new.df) = paste(name.df,"diff",sep ="_")
  result = cbind(df,new.df)
  return(result)
}

但是我得到的输出都是错误的。它不像我想要的那样做减法或除法。

【问题讨论】:

    标签: r function dataframe


    【解决方案1】:

    你的问题是(x-Mean)/Mean的部分; Mean 不存在于您可能指的是mean(x) 的任何地方。

    new.function <- function(df){
      name.df<- colnames(df)
      new.df <- apply(df, MARGIN=2, FUN=function(x) (x-mean(x))/mean(x))
      colnames(new.df) <- paste(name.df, "diff", sep ="_")
      result <- cbind(df, new.df)
      return(result)
    }
    
    new.function(df)
    #        GDP per_capita GDP_diff per_capita_diff
    # France   2          5     -0.5            -0.5
    # Spain    4         10      0.0             0.0
    # Italy    6         15      0.5             0.5
    # Mean     4         10      0.0             0.0
    

    数据:

    df <- structure(list(GDP = c(2L, 4L, 6L, 4L), per_capita = c(5L, 10L, 
    15L, 10L)), class = "data.frame", row.names = c("France", "Spain", 
    "Italy", "Mean"))
    

    【讨论】:

      【解决方案2】:

      data.table 方法:

      x <- data.frame(GDP = c(2,4,6), per_capita=c(5,10,15))
      rownames(x) <- c("F", "ES", "IT")
      
      library(data.table)
      
      setDT(x)
      x[,`:=`(GDP_diff = (GDP-mean(GDP, na.rm=T))/mean(GDP, na.rm=T),
              per_capita_diff = (per_capita-mean(per_capita, na.rm=T))/mean(per_capita, na.rm=T))]
      

      【讨论】:

        【解决方案3】:

        尝试使用 dplyr 中的 mutate() 直接计算避免循环的变量:

        library(dplyr)
        library(tidyr)
        #Code
        new <- df %>%
          mutate(GDP_diff=(GDP-mean(GDP))/mean(GDP),
                 per_capita_diff=(per_capita-mean(per_capita))/mean(per_capita))
        

        输出:

          GDP per_capita GDP_diff per_capita_diff
        1   2          5     -0.5            -0.5
        2   4         10      0.0             0.0
        3   6         15      0.5             0.5
        4   4         10      0.0             0.0
        

        使用的一些数据:

        #Data
        df <- structure(list(GDP = c(2L, 4L, 6L, 4L), per_capita = c(5L, 10L, 
        15L, 10L)), class = "data.frame", row.names = c("France", "Spain", 
        "Italy", "Mean"))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-07
          • 1970-01-01
          • 1970-01-01
          • 2023-01-12
          • 2021-06-13
          • 1970-01-01
          • 2019-07-05
          • 2021-02-21
          相关资源
          最近更新 更多