为什么轻拍将子集作为NA而不完全排除它们 [英] Why does tapply take the subset as NA and not exclude them totally

查看:122
本文介绍了为什么轻拍将子集作为NA而不完全排除它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.我想制作一个均值和误差线的条形图,其中将其分为两个因素.为了获得均值和标准误差,我使用tapply函数.

I have a question. I want to make a barplot with the mean and errorbars, where it is grouped for two factors. To get the mean and the standard errors I used the function tapply.

但是我想降低一级的因素之一.

However for one of the factor I want to drop one level.

所以我做了什么:

dataFE <- data[-which(plant=="FS"),] # this works fine, I get exactly the data set I want without the FS level of the factor plant 

然后使用平均值和标准误差:

Then to get the mean and standard error I use this:

means <- with(dataFE, as.matrix(tapply(leaves, list(plant, Orchestia), mean), nrow=2)

e <- with(dataFE, as.matrix(tapply (leaves, list(plant, Orchestia), function(x) sd(x)/sqrt(length(x))), nrow=2))

发生了一些奇怪的事情,它不计算FS,但是将其放在带有NA的表中:

And there something strange happens, it does not calculate the FS, however it puts it in a table with NA:

    row.names   no          yes
1   F           7.009022    5.307185

2   FS          NA          NA

3   S           2.837139    2.111054

这是我不想要的,因为如果我在barplot2(软件包gplots)中使用它,那么我会得到一个用于FS的空白栏,而那个绝对不应该存在.

This I don't want, cause if I use this in barplot2 (package gplots) then I will get an empty bar for the FS, whereas that one should not be there at all.

因此,任何使用者都有解决方案或其他方法来获得漂亮的barplot :).不管怎么说,还是要谢谢你!

So does any of use have a solution or an other method to get a nice barplot :). Thanks any way!

推荐答案

在没有数据样本的情况下,我只是猜测一下:

Without a sample of your data, I'll just wager a guess:

您的色谱柱装置是一个因素.并且,当您删除具有该值的行时,级别" FS仍然存在.使用levels(data$plant)进行查看.然后,您可以使用droplevels摆脱它.

your column plant is a factor. And while you have dropped the rows that have that value, the "level" FS still exists. Use levels(data$plant) to see. You can then use droplevels to get rid of it.

dat <- data.frame(x=1:15, y=factor(letters[1:3]))

> levels(dat$y)
[1] "a" "b" "c"

dat <- dat[dat$y != 'a',]
> levels(dat$y)
[1] "a" "b" "c"
> 

> tapply(dat$x, dat$y, sum)
 a  b  c 
NA 40 45 
> 

> droplevels(dat$y)
 [1] b c b c b c b c b c
Levels: b c
> dat$y <- droplevels(dat$y)

> tapply(dat$x, dat$y, sum)
 b  c 
40 45 
> 

这篇关于为什么轻拍将子集作为NA而不完全排除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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