如何将中间值添加到行? [英] how to add median value to rows?

查看:92
本文介绍了如何将中间值添加到行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含53列(ID列加52个数字值)的数据框(数据).我可以用以下内容添加均值列:

I have a data frame (data) with 53 columns (ID column plus 52 numeric values). I can add a column with mean with the following:

data$mean <- rowMeans(data[,2:51],na.rm = TRUE) # add mean of rows

但是,我不能通过以下方式对中位数做同样的事情:

But, I cannot do the same for median with the following:

data$medians <- rowMedians(month.sum[,2:51],na.rm = TRUE) # add medians of rows

我安装了包含rowMedians的软件包 matrixStats ,但对我不起作用.

I installed the package matrixStats which includes rowMedians, but it does not work for me.

推荐答案

您可以使用apply将任意函数应用于行.使用

You can apply arbitrary functions to rows with apply. Use

apply(data[,2:51],1, median, na.rm = TRUE)

阅读?apply帮助页面以获取更多信息.

Read the ?apply help page for more information.

这篇关于如何将中间值添加到行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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