在R中另一列的值为1的情况下获取列的中位数 [英] Getting Median of a Column where value of another Column is 1 in R

查看:109
本文介绍了在R中另一列的值为1的情况下获取列的中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我有一个与此结构类似的csv文件

Ok so I have a csv file similar to this structure

hashID,value,flag

98fafd,   35,   1

fh56w2,   25,   0

ggjeas,   55,   1

adfh5d,   45,   0

基本上我想做的是获取值列的中位数,但只在计算中包含flag==1的行.

Basically what I want to do is get the median of the value column but only include rows where flag==1 in the calculation.

在R中甚至有可能吗?我到处搜寻,却找不到类似的东西.

Is this even possible in R? I've searched around and haven't found anything like this.

推荐答案

这里是一种可能:

使用以下命令读取数据集:

Read your data set using the following command:

newdata <- read.csv("stackoverflow questions/mediancol.csv")
# I assume you have the data in csv format

   # Showing the data I used for the computation
     newdata <- structure(list(hashID = structure(c(1L, 3L, 4L, 2L), .Label = c("98fafd", 
"adfh5d", "fh56w2", "ggjeas"), class = "factor"), value = c(35L, 
25L, 55L, 45L), flag = c(1L, 0L, 1L, 0L)), .Names = c("hashID", 
"value", "flag"), class = "data.frame", row.names = c(NA, -4L
))
    > newdata
  hashID value flag
1 98fafd    35    1
2 fh56w2    25    0
3 ggjeas    55    1
4 adfh5d    45    0

# Subset the data when flag =1
newdata1 <- subset(newdata,flag==1)

# Look at the summary of the data

> summary(newdata1)
    hashID      value         flag  
 98fafd:1   Min.   :35   Min.   :1  
 adfh5d:0   1st Qu.:40   1st Qu.:1  
 fh56w2:0   Median :45   Median :1  
 ggjeas:1   Mean   :45   Mean   :1  
            3rd Qu.:50   3rd Qu.:1  
            Max.   :55   Max.   :1

# Only look at the median 
median(newdata1$value)
[1] 45

这篇关于在R中另一列的值为1的情况下获取列的中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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