如何获取专栏行的具体行? [英] How to get column mean for specific rows only?

查看:85
本文介绍了如何获取专栏行的具体行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到特定行(这里:年)的一列(这里:得分)的平均值。具体来说,我想知道三个时期的平均分:




  • 期间1:年< = 1983

  • period 2:year> = 1984&年< = 1990

  • 期间3:年> = 1991



的数据:

 国家年份
阿尔及利亚1980 -1.1201501
阿尔及利亚1981 -1.0526943
阿尔及利亚1982 -1.0561565
阿尔及利亚1983 -1.1274560
阿尔及利亚1984 -1.1353926
阿尔及利亚1985 -1.1734330
阿尔及利亚1986 -1.1327666
阿尔及利亚1987 -1.1263586
阿尔及利亚1988 -0.8529455
阿尔及利亚1989 -0.2930265
阿尔及利亚1990 -0.1564207
阿尔及利亚1991 -0.1526328
阿尔及利亚1992 -0.9757842
阿尔及利亚1993 -0.9714060
阿尔及利亚1994 - 1.1422258
阿尔及利亚1995 -0.3675797
...

计算的平均值应为添加到附加列(平均值)中的df,即期间1的相同平均值,对于期间2等的值。



这是如何它应该如下所示:

 国家年份平均值
阿尔及利亚1980 -1.1201501 -1.089
阿尔及利亚1981 -1.0526943 -1.089
阿尔及利亚1982 -1.0561565 -1.089
阿尔及利亚1983 -1.1274560 -1.089
阿尔及利亚1984 -1.1353926 -0.839
阿尔及利亚1985 -1.1734330 -0.839
阿尔及利亚1986 -1.1327666 -0.839
阿尔及利亚1987 -1.1263586 -0.839
阿尔及利亚1988 -0.8529455 -0.839
阿尔及利亚1989 -0.2930265 -0.839
阿尔及利亚1990 -0.1564207 -0.839
...
pre>

我尝试的每一个可能的路径都很容易超级复杂 - 我必须计算90多个国家不同时期的平均分数...



非常感谢您的帮助!

解决方案

 <$ c $ (datfrm,ave(score,findInterval(year,c(-Inf,1984,1991,Inf)),fFS = mean))

标题问题与真正的问题有点不同,将通过使用逻辑索引来回答。如果一个人想要一个特定子集的意思,说 year> = 1984&年< = 1990 将通过以下方式完成:

  mn84_90<  - 与(datfrm, mean(score [year> = 1984& year< = 1990]))


I need to get the mean of one column (here: score) for specific rows (here: years). Specifically, I would like to know the average score for three periods:

  • period 1: year <= 1983
  • period 2: year >= 1984 & year <= 1990
  • period 3: year >= 1991

This is the structure of my data:

  country year     score        
 Algeria 1980     -1.1201501 
 Algeria 1981     -1.0526943 
 Algeria 1982     -1.0561565 
 Algeria 1983     -1.1274560 
 Algeria 1984     -1.1353926 
 Algeria 1985     -1.1734330 
 Algeria 1986     -1.1327666 
 Algeria 1987     -1.1263586 
 Algeria 1988     -0.8529455 
 Algeria 1989     -0.2930265 
 Algeria 1990     -0.1564207 
 Algeria 1991     -0.1526328 
 Algeria 1992     -0.9757842 
 Algeria 1993     -0.9714060 
 Algeria 1994     -1.1422258 
 Algeria 1995     -0.3675797 
 ...

The calculated mean values should be added to the df in an additional column ("mean"), i.e. same mean value for years of period 1, for those of period 2 etc.

This is how it should look like:

country year     score         mean   
 Algeria 1980     -1.1201501     -1.089
 Algeria 1981     -1.0526943     -1.089
 Algeria 1982     -1.0561565     -1.089
 Algeria 1983     -1.1274560     -1.089
 Algeria 1984     -1.1353926     -0.839
 Algeria 1985     -1.1734330     -0.839
 Algeria 1986     -1.1327666     -0.839
 Algeria 1987     -1.1263586     -0.839
 Algeria 1988     -0.8529455     -0.839
 Algeria 1989     -0.2930265     -0.839
 Algeria 1990     -0.1564207     -0.839
 ...

Every possible path I tried got easily super complicated - and I have to calculate the mean scores for different periods of time for over 90 countries ...

Many many thanks for your help!

解决方案

datfrm$mean <-
  with (datfrm, ave( score, findInterval(year, c(-Inf, 1984, 1991, Inf)), FUN= mean) )

The title question is a bit different than the real question and would be answered by using logical indexing. If one wanted only the mean for a particular subset say year >= 1984 & year <= 1990 it would be done via:

mn84_90 <- with(datfrm, mean(score[year >= 1984 & year <= 1990]) )

这篇关于如何获取专栏行的具体行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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