如何在R中的数据框中有条件地选择列

时间:2014-02-06 03:49:55

标签: r csv statistics dataframe import-from-csv

我如何找到女性的平均值/中位数(任何其他此类事物)?我尝试了一些代码来访问女性数据,但没有成功。任何帮助都非常感谢。

> jalal <- read.csv("jalal.csv", header=TRUE,sep=",")
> which(jalal$sex==F)
integer(0)
> jalal
   age sex weight eye.color hair.color
1   23   F   93.8      blue      black
2   21   M  180.8     amber       gray
3   22   F  196.5     hazel       gray
4   22   M  256.2     amber      black
5   21   M  219.6      blue       gray
6   16   F  152.1      blue       gray
7   21   F  183.3      gray   chestnut
8   18   M  179.1     brown      blond
9   15   M  206.1      blue      white
10  19   M  211.6     brown      blond
11  20   F  209.4      blue      white
12  21   M  194.0     brown     auburn
13  22   F  204.1     green      black
14  21   F  157.4     hazel        red
15  15   F  238.0     green       gray
16  20   F  154.8      gray       gray
17  16   F  245.8      gray       gray
18  23   M  198.2      gray        red
19  19   M  169.1     green      brown
20  24   M  198.0     green       gray
> subset(jalal, subset=(sex =F)) -> females
> females
[1] age        sex        weight     eye.color  hair.color
<0 rows> (or 0-length row.names)
> subset(jalal, subset=(sex ==F)) -> females
> females
[1] age        sex        weight     eye.color  hair.color
<0 rows> (or 0-length row.names)

这是jalal.csv中的内容:

"age","sex","weight","eye.color","hair.color"
23,"F",93.8,"blue","black"
21,"M",180.8,"amber","gray"
22,"F",196.5,"hazel","gray"
22,"M",256.2,"amber","black"
21,"M",219.6,"blue","gray"
16,"F",152.1,"blue","gray"
21,"F",183.3,"gray","chestnut"
18,"M",179.1,"brown","blond"
15,"M",206.1,"blue","white"
19,"M",211.6,"brown","blond"
20,"F",209.4,"blue","white"
21,"M",194,"brown","auburn"
22,"F",204.1,"green","black"
21,"F",157.4,"hazel","red"
15,"F",238,"green","gray"
20,"F",154.8,"gray","gray"
16,"F",245.8,"gray","gray"
23,"M",198.2,"gray","red"
19,"M",169.1,"green","brown"
24,"M",198,"green","gray"

3 个答案:

答案 0 :(得分:5)

您正在寻找aggregate。这是一个论坛,按性别返回中位年龄和体重:

aggregate(cbind(age, weight) ~ sex, data=jalal, FUN=median)
##   sex  age weight
## 1   F 20.5  189.9
## 2   M 21.0  198.1

要获取仅包含女性的数据框,以下是[的语法:

jalal[jalal$sex == 'F',]

请注意'F'周围的引号。裸F表示FALSE。这就是你的第二个subset表达式失败的原因。

subset(jalal, subset=(sex =='F'))
##    age sex weight eye.color hair.color
## 1   23   F   93.8      blue      black
## 3   22   F  196.5     hazel       gray
## 6   16   F  152.1      blue       gray

...

在评论中,要求提供蓝眼女性的平均值的方法。第一种方法是将数据框过滤到蓝眼睛的人:

aggregate(cbind(age, weight) ~ sex, data=jalal[jalal$eye.color == 'blue',], FUN=mean)
##   sex      age   weight
## 1   F 19.66667 151.7667
## 2   M 18.00000 212.8500

但这似乎是hackish,毕竟,我们并没有过滤女性的数据框架。因此,这是一个公式,通过性别和眼睛颜色给出平均年龄和体重。从这里,你可以找到蓝眼睛的女人,绿眼睛的男人等的平均值:

aggregate(cbind(age, weight) ~ sex + eye.color, data=jalal, FUN=mean)
##   sex eye.color      age   weight
## 1   M     amber 21.50000 218.5000
## 2   F      blue 19.66667 151.7667
## 3   M      blue 18.00000 212.8500
## 4   M     brown 19.33333 194.9000
## 5   F      gray 19.00000 194.6333
## 6   M      gray 23.00000 198.2000
## 7   F     green 18.50000 221.0500
## 8   M     green 21.50000 183.5500
## 9   F     hazel 21.50000 176.9500

注意,这里的第2行和第3行与前一个表达式中的结果相匹配。

答案 1 :(得分:1)

以下是使用data.table包的替代解决方案:

require(data.table)
jalal <- as.data.table(jalal)

关于女性的子集​​:

jalal[sex == "F"]

计算平均值,中位数等:

> jalal[sex == "F", mean(weight)]
[1] 183.52
> jalal[sex == "F", list(mean(weight), median(age))]
       V1   V2
1: 183.52 20.5

答案 2 :(得分:0)

这样您就可以看到所有主要选项,这是dplyr的解决方案:

library(dplyr)
jalal %.% 
  group_by(sex, eye.color) %.%
  summarise(age = mean(age), weight = median(weight))