本文介绍了如何计算特定国家/地区的利润中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好, 我是R的新手,正在尝试在数据框中计算特定国家/地区的利润中位数.我尝试了一个国家/地区以下的收入,但对我而言不起作用.
Hello folks, I am new to R and I am trying to compute median profit for a particular country in a data frame.I tried below one but it doesn't work for me.
data("Forbes2000", package = "HSAUR")
median(Forbes2000[,"sales","country"="United States"])
推荐答案
median(Forbes2000$sales[Forbes2000$country == "United States"])
尽管不知道您的数据框是什么样子很难确定.如果您想获取一个包含每个国家(而不是一个国家)中位数的data.frame,则可以执行以下操作:
Though it's hard to be certain without knowing what your data frame looks like. If you want to get a data.frame with the median of every country instead of just one, you could do:
library(plyr)
ddply(Forbes2000, "country", function(d) median(d$sales))
(您必须首先安装 plyr 软件包,例如,通过执行install.packages("plyr")
)
(You would have to install the plyr package first, for example by doing install.packages("plyr")
).
这篇关于如何计算特定国家/地区的利润中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!