基于R中不同数据帧的值的子集 [英] Subsetting based on values of a different data frame in R

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

问题描述

如果行中的每个值都大于不同数据帧中的相应行,则我想要子集数据。我也需要跳过一些顶行。以前的这些问题并没有帮助我,但是这是相关的:



根据另一个数据帧的内容对数据帧进行子集



使用来自不同数据帧的信息的子集数据[r] a>

 > A 
name1 name2
cond trt ctrl
小时0 3
A 1 1
B 10 1
C 1 1
D 1 1
E 10 10
> B
name1 name2
cond trt ctrl
小时0 3
A 1 1
B 1 10
C 1 1
D 1 1
E 1 1

我想要这个。只有A中所有值都大于B的行:

  name1 name2 
cond trt ctrl
hour 0 3
E 10 10

我尝试了这3行:

 子集(A,TRUE,select =(A [3:7,]> B [3:7,]))
子集(A,A> B)
A [A [3:7,]> B [3:7,]]

非常感谢。以下是生成数据的代码:

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



#############后续问题2/28/13

基于R中不同数据帧的调整值进行子集化时的错误

解决方案

  N<  -  nrow(A)
cond< - sapply(3: N,函数(i)sum(A [i, ] B [i,])== 2)
rbind(A [1:2,],子集(A [3:N,],cond))


I want to subset data if every value in the row is greater than the respective row in a different data frame. I also need to skip some top rows. These previous questions did not help me, but it is related:

Subsetting a data frame based on contents of another data frame

Subset data using information from a different data frame [r]

> A
     name1 name2
cond   trt  ctrl
hour     0     3
A        1     1
B       10     1
C        1     1
D        1     1
E       10    10
> B
     name1 name2
cond   trt  ctrl
hour     0     3
A        1     1
B        1    10
C        1     1
D        1     1
E        1     1

I want this. Only rows where ALL values were greater in A than B:

     name1 name2
cond   trt  ctrl
hour     0     3
E       10    10

I've tried these 3 lines:

subset(A, TRUE, select=(A[3:7,] > B[3:7,]))
subset(A, A > B)
A[A[3:7,] > B[3:7,]]

Thanks so much. Here is the code to generate the data:

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", "1"), 
    name2 = c("ctrl", "3", "1", "10", "1", "1", "1")), .Names = c("name1", 
"name2"), row.names = c("cond", "hour", "A", "B", "C", "D", "E"
), class = "data.frame")

############# Follow-up question asked 2/28/13

Error when subsetting based on adjusted values of different data frame in R

解决方案

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

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

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