【发布时间】:2016-12-25 05:01:51
【问题描述】:
几天以来,我一直在寻找在 R 中管理数据的方法。我有一组相同的个体 (n=5013),其结构如下:两个不对称邻接矩阵(m1 和 m2)( nxn 个方阵,其中所有个体组成矩阵的行和列)和一个数据框(df),其中包含我的一组个体(df$N)和一个变量(df$V)。
我正在搜索使用变量df$V(行和列的不同标准/变量值)和子集m1(或识别无效案例)在@987654328 的单元格值的函数中对矩阵进行子集化的方法@。
下面的例子说明了我的问题:
# N are individuals. Two matrices (m1 and m2) and a dataframe (df) with a variable (df$V)
> df
N V
1 a v1
2 b v2
3 c v3
4 d v1
5 e v2
6 f v3
7 g v1
> m1
a b c d e f g
a 7 3 9 8 1 6 8
b 1 6 9 2 9 4 4
c 2 3 2 7 9 7 3
d 9 7 6 3 2 6 6
e 9 9 6 5 5 6 5
f 1 1 1 6 1 5 9
g 6 2 5 2 1 8 5
> m2
a b c d e f g
a 8 3 7 8 4 3 2
b 2 8 4 2 7 7 2
c 8 3 1 6 9 9 4
d 7 3 6 7 4 9 5
e 5 8 7 1 7 6 6
f 9 6 8 9 6 6 2
g 4 8 8 1 9 7 3
例如,我对矩阵中的单元格进行子集化,其中 rows 采用值“v1”和“v3”,cols 采用 df$V 中的值“v2”
> m1subseted
b e
a 3 1
c 3 9
d 7 2
f 1 1
g 2 1
> m2subseted
b e
a 3 4
c 3 9
d 3 4
f 6 6
g 8 9
然后在 m1-subseted 子集中观察(或识别无效案例)在 m2-subseted 中具有单元格值“
#subset m1 if cell value in m2 is <5 / Invalid cells = NA
b e
a 3 1
c 3 NA
d 7 2
f NA NA
g NA NA
可重现的数据
m1 <- as.matrix(data.frame(a = sample(1:10, size = 7),
b= sample(1:10, size = 7),
c=sample(1:10, size = 7),
d=sample(1:10, size = 7),
e=sample(1:10, size = 7),
f=sample(1:10, size = 7),
g=sample(1:10, size = 7)))
rownames(m1)<-colnames(m1)
m2 <- as.matrix(data.frame(a = sample(1:10, size = 7),
b= sample(1:10, size = 7),
c=sample(1:10, size = 7),
d=sample(1:10, size = 7),
e=sample(1:10, size = 7),
f=sample(1:10, size = 7),
g=sample(1:10, size = 7)))
rownames(m2)<-colnames(m2)
df <- data.frame(N = as.factor(letters[1:7]),
V = c("v1","v2","v3","v1","v2","v3","v1"))
评论
@jkt 提出的解决方案效果很好,除非标签很复杂(带有重音符号、括号等),如我的原始数据集中。我找到的解决方案是在应用算法之前将复杂的标签更改为最简单的标签,并在结果上恢复原始标签。 我与@jkt 提供的解决方案(适用于示例)共享我使用的代码,希望它对某人有用。
#Create new labels. In this case are numbers, where 7
#correspond to the dimmensions of matrices and observations on df
new.code.labels<-c(1:7)
#Create new col/variable on df
df$TempLabel<-new.code.labels
#Recode rows and cols on matrices
rownames(m1)<-new.code.labels
colnames(m1)<-new.code.labels
rownames(m2)<-new.code.labels
colnames(m2)<-new.code.labels
#Apply algorithm proposed by @jkt
crit1 <- c('v1','v3')
crit2 <- 'v2'
#Observe I use new labels on dataframe (df$TempLabel)
m11 <- m1[df$TempLabel[which(df$V %in% crit1)], df$TempLabel[which(df$V %in% crit2)]]
m21 <- m2[df$TempLabel[which(df$V %in% crit1)], df$TempLabel[which(df$V %in% crit2)]]
m11[!(m21<5)] <- NA
m11
#To regain the original labels on results
row.coded.labels.result<-rownames(m11)
df.subseted.by.result.row<-subset(df, df$TempLabel %in% row.coded.labels.result)
rownames(m11)<-df.subseted.by.result.row$N
col.coded.labels.result<-colnames(m11)
df.subseted.by.result.col<-subset(df, df$TempLabel %in% col.coded.labels.result)
colnames(m11)<-df.subseted.by.result.col$N
m11
【问题讨论】:
-
“df”中的
N是什么?它是否对应于 'm1/m2' 的行名? -
也有助于提供一个可重复的例子......与实际数据或类似数据
-
嗨@akrun。 N 是标签,它们对应于 m1/m2 Hi @CyrusMohammadian 的行名和列名。这是我用来创建示例的代码
N <- c("a", "b", "c", "d", "e", "f", "g") # My population V <-c("v1","v2","v3","v1", "v2", "v3", "v1") #The variable m1 <- matrix(sample.int(9, size = 7*7, replace = TRUE), nrow=7, ncol=7) m2 <- matrix(sample.int(9, size = 7*7, replace = TRUE), nrow=7, ncol=7) colnames(m1)<-N rownames(m1)<-N colnames(m2)<-N rownames(m2)<-N df<-data.frame(N,V)感谢您的关注!
标签: r matrix dataframe subset adjacency-matrix