我有以下数据帧(Df1)。此数据帧包含名为" mean“的行,该行具有每列的平均值。
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我必须假设将使用此函数的每个数据帧都有一个名为"Mean“的行。到目前为止,我得到的是:
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)
}然而,我得到的输出都是错误的。它没有像我想要的那样做减法或除法。
发布于 2020-12-28 20:54:24
尝试使用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"))发布于 2020-12-28 20:54:32
您的问题是(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"))发布于 2020-12-28 20:57:54
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))]https://stackoverflow.com/questions/65477807
复制相似问题