如何在 R 中找到 DataFrame 列的平均值?
在本文中,我们将讨论如何在 R 编程语言中计算 Dataframe Column 的平均值。
它可以通过多种方式完成:
- 使用 $-Operator
- 使用 [[]]
- 使用列索引
- 使用 dplyr 包的汇总函数
- 使用 colMeans函数
方法一:使用$-Operator。
这是最简单的方法之一,因为在这种计算给定数据帧列的均值的函数,只需要调用 R 语言内置函数的均值函数并传递带有列名称的 $-运算符其中 mean 将作为函数的参数进行计算,作为回报,该函数将使用 $-运算符返回所提供列的平均值。
均值函数:此函数的计算方法是将值的总和除以数据系列中的值数。
Syntax: mean(x, trim = 0, na.rm = FALSE, …)
Parameters:
- x is the input vector.
- trim is used to drop some observations from both ends of the sorted vector.
- na.rm is used to remove the missing values from the input vector.
例子:
在这个例子中,我们将使用 R 语言中带有 $-operation 的 mean()函数计算给定数据帧第三列的平均值。
R
gfg_data = data.frame(x1 = c(1, 2, 3, 4, 5),
x2 = c(8, 4, 5, 1, 2),
x3 = c(7, 9, 1, 2, 7))
mean(gfg_data$x3)
R
gfg_data = data.frame(x1 = c(1, 2, 3, 4, 5),
x2 = c(8, 4, 5, 1, 2),
x3 = c(7, 9, 1, 2, 7))
mean(gfg_data[["x3"]])
R
gfg_data = data.frame(x1 = c(1, 2, 3, 4, 5),
x2 = c(8, 4, 5, 1, 2),
x3 = c(7, 9, 1, 2, 7))
mean(gfg_data[ , 3])
R
library("dplyr")
gfg_data = data.frame(x1 = c(1, 2, 3, 4, 5),
x2 = c(8, 4, 5, 1, 2),
x3 = c(7, 9, 1, 2, 7))
summarise(gfg_data, gfg_mean = mean(x3))
R
gfg_data = data.frame(x1 = c(1, 2, 3, 4, 5),
x2 = c(8, 4, 5, 1, 2),
x3 = c(7, 9, 1, 2, 7))
colMeans(gfg_data)
输出:
5.2
方法二:使用[[]]。
在这种计算给定数据帧列的均值的方法中,用户需要调用 mean()函数,作为其参数,用户将使用 [[]] 并传递均值的数据帧列的名称将被计算,这将以 r 语言将数据帧提供的列的平均值返回给用户。
在这个例子中,我们将在 r 语言中使用带有 [[]] 的 mean()函数计算给定数据帧的第三列的平均值(与前面的例子相同)。
电阻
gfg_data = data.frame(x1 = c(1, 2, 3, 4, 5),
x2 = c(8, 4, 5, 1, 2),
x3 = c(7, 9, 1, 2, 7))
mean(gfg_data[["x3"]])
输出:
5.2
方法三:使用列索引。
在这种计算方法中,给定数据帧的均值用户需要调用均值函数并将要计算均值的列的列索引作为函数的参数传递,这个过程将返回平均值给提供的列索引的用户作为参数。
在这个例子中,我们将使用一个 mean()函数和 r 语言中的列索引来计算给定数据帧的第三列的平均值(与前面的例子相同)。
电阻
gfg_data = data.frame(x1 = c(1, 2, 3, 4, 5),
x2 = c(8, 4, 5, 1, 2),
x3 = c(7, 9, 1, 2, 7))
mean(gfg_data[ , 3])
输出:
5.2
方法四:使用dplyr包的summary函数。
在这种计算给定数据帧列的均值的方法中,用户首先需要安装和加载 dplyr 包并从该包中调用汇总函数并将所需的参数传递给该函数,此过程将导致返回均值函数参数中提供的列。
汇总函数:此函数通常用于分组数据并根据指定的参数工作
Syntax: summarise(.data, …)
Parameters:
- .data:-A tbl. All main verbs are S3 generics and provide methods for tbl_df(), dtplyr::tbl_dt() and dbplyr::tbl_dbi().
- …:-Name-value pairs of summary functions. The name will be the name of the variable in the result.
在此示例中,我们使用 r 语言中的 dplyr 包的汇总函数计算给定数据帧的第三列的平均值(与前一个示例相同)。
电阻
library("dplyr")
gfg_data = data.frame(x1 = c(1, 2, 3, 4, 5),
x2 = c(8, 4, 5, 1, 2),
x3 = c(7, 9, 1, 2, 7))
summarise(gfg_data, gfg_mean = mean(x3))
输出:
gfg_mean
1 5.2
方法 5:使用 colMeans函数。
在这种计算方法中,给定数据框列的平均值用户只需要调用 colMeans函数,该函数是 R 语言中的内置函数并将数据框作为其参数传递,然后这将返回所有列的平均值在提供给用户的数据框中。
colMeans()函数:此函数有助于计算分组变量的每个级别的类似数字矩阵的对象的列之间的行(加权)均值。
Syntax: colmean(M, group = colnames(M), w = FALSE, reord = FALSE,na_rm = FALSE, big = TRUE, …)
Parameters:
- M:-a matrix, dataframe or vector of numeric data. Missing values are allowed.
- group:-a vector or factor giving the grouping, with one element per row of M.
- w:-a vector giving the weights that must be applied to each of the stacked blocks of an original object
- reord:-if TRUE, then the result will be in order of sort(unique(group)), if FALSE (the default), it will be in the order that groups were encountered.
- na_rm:-logical (TRUE or FALSE). Should NA (including NaN) values be discarded?
- big:-is your object big and integer overflow is likely.
- …:-other arguments to be passed to or from methods.
在此示例中,我们使用 R 语言中的 colMeans()函数计算给定数据帧的第三列的平均值(与前一个示例相同)。
电阻
gfg_data = data.frame(x1 = c(1, 2, 3, 4, 5),
x2 = c(8, 4, 5, 1, 2),
x3 = c(7, 9, 1, 2, 7))
colMeans(gfg_data)
输出:
X1 3
X2 4
X3 5.2