如何基于另一个数据集对数据集进行子集化?

时间:2015-12-27 21:35:48

标签: r matrix

我希望对满足以下要求的数据框z进行子集化:

1,从数据框z中删除观察到第1列的值不在矩阵y的行名中;

2,从数据框z中删除观察结果,即第2列的值不在矩阵y的列名中。

以下是创建数据框z和矩阵y的代码:

    ## a matrix y 

y<-matrix(1:12, nrow=3, ncol=4, dimnames = list(c("R1", "R2","R3"),c("C1", "C2","C3","C4") ))


## a dataframe z
col1<-c("R1", "R2","R3","R4","R5","R6","R7","R8","R9")
col2<-c("C1", "C2","C3","C4","C5","C6","C7","C8","C9")


z<- data.frame(col1,col2)

datafram z的形式是: data frame z

矩阵y的形式是: matrix y

我想要的输出数据帧应该是:

          col1     col2

1          R1       C1

2          R2       C2

3          R3       C3

我不知道如何根据矩阵的行名和列名对数据帧进行子集化。谁能知道怎么做?提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

# Specification of z and y:

y<-matrix(1:12, nrow=3, ncol=4, dimnames = list(c("R1", "R2","R3"),c("C1", "C2","C3","C4") ))

col1<-c("R1", "R2","R3","R4","R5","R6","R7","R8","R9")
col2<-c("C1", "C2","C3","C4","C5","C6","C7","C8","C9")

z<- data.frame(col1,col2)

# extract subset:

col.in.y.row = ( z[,1] %in% row.names(y) ) 

col.in.y.col = ( z[,2] %in% colnames(y) )

data.frame(z, col.in.y.row, col.in.y.col)

(z.subsetted = z[ col.in.y.row & col.in.y.col, ])