基于R中不同数据帧的调整值进行子集化时出错 [英] Error when subsetting based on adjusted values of different data frame in R

查看:469
本文介绍了基于R中不同数据帧的调整值进行子集化时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问一个关于我从@redmode学到的方法的问题:

I am asking a side-question about the method I learned here from @redmode :

中不同数据帧的值的子集设置

Subsetting based on values of a different data frame in R

当我尝试通过以下方式动态调整我想要的子级别:

When I try to dynamically adjust the level I want to subset by:

N <- nrow(A)
cond <- sapply(3:N, function(i) sum(A[i,] > 0.95*B[i,])==2)
rbind(A[1:2,], subset(A[3:N,], cond))

我收到错误

Error in FUN(left, right) : non-numeric argument to binary operator. 

你能想到一种方式可以获得与A中大于95%的值相关的行的价值在B?谢谢。

Can you think of a way I can get rows pertaining to values in A that are greater than 95% of the value in B? Thank you.

这里是A和B的代码。

A <- structure(list(name1 = c("trt", "0", "1", "10", "1", "1", "10"
), name2 = c("ctrl", "3", "1", "1", "1", "1", "10")), .Names = c("name1", 
"name2"), row.names = c("cond", "hour", "A", "B", "C", "D", "E"
), class = "data.frame")
B <- structure(list(name1 = c("trt", "0", "1", "1", "1", "1", "9.4"), 
    name2 = c("ctrl", "3", "1", "10", "1", "1", "9.4")), .Names = c("name1", 
"name2"), row.names = c("cond", "hour", "A", "B", "C", "D", "E"
), class = "data.frame")


推荐答案

您的数据存在严重的格式化问题。

You have some serious formatting issues with your data.

首先,列应该是相同的数据类型,行应该是观察。 (不一定是真的,但是一个非常好的开始)在这里你有一个名为 cond 的行,然后一行叫做小时,然后一系列的分类我猜。以数据的方式开始,并没有什么意义,而且不易于操纵数据。但一切都不会丢失。这是我会做的:

First, columns should be of the same data type, rows should be observations. (not always true, but a very good way to start) Here you have a row called cond, then a row called hour, then a series of classifications I'm guessing. The way you're data is presented to begin with doesn't make much sense and doesn't lend itself to easy manipulation of your data. But all is not lost. This is what I would do:

重组我的数据:

C <- data.frame(matrix(as.numeric(unlist(A)), ncol=2)[-(1:2), ])

colnames(C) <- c('A.trt', 'A.cntr')
rownames(C) <- LETTERS[1:nrow(C)]

D <- data.frame(matrix(as.numeric(unlist(B)), ncol=2)[-(1:2), ])

colnames(D) <- c('B.trt', 'B.cntr')

(df <- cbind(C, D))

其中:

#   A.trt A.cntr B.trt B.cntr
# A     1      1   1.0    1.0
# B    10      1   1.0   10.0
# C     1      1   1.0    1.0
# D     1      1   1.0    1.0
# E    10     10   9.4    9.4

然后你的问题很容易解决:

Then you're problem is easily solved by:

df[which(df[, 1] > 0.95*df[, 3] & df[, 2] > 0.95*df[, 4]), ]

#   A.trt A.cntr B.trt B.cntr
# A     1      1   1.0    1.0
# C     1      1   1.0    1.0
# D     1      1   1.0    1.0
# E    10     10   9.4    9.4

这篇关于基于R中不同数据帧的调整值进行子集化时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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