给定列名,数据框中列的均值 [英] Mean of a column in a data frame, given the column's name

查看:68
本文介绍了给定列名,数据框中列的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个必须编写的大函数中.在最后一部分中,我必须计算数据帧中一列的平均值.我正在处理的列的名称作为该函数的参数给出.

I'm inside a big function I have to write. In the last part I have to calculate the mean of a column in a data frame. The name of the column I am operating on is given as an argument to the function.

推荐答案

我认为您在问如何根据给定的列名计算数据框中变量的均值.有两种典型的方法,一种使用[[进行索引,另一种使用[进行索引:

I think you're asking how to compute the mean of a variable in a data frame, given the name of the column. There are two typical approaches to doing this, one indexing with [[ and the other indexing with [:

data(iris)
mean(iris[["Petal.Length"]])
# [1] 3.758
mean(iris[,"Petal.Length"])
# [1] 3.758
mean(iris[["Sepal.Width"]])
# [1] 3.057333
mean(iris[,"Sepal.Width"])
# [1] 3.057333

这篇关于给定列名,数据框中列的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆