用matrix替换data.frame

时间:2011-03-11 09:38:31

标签: r dataframe

我无法在网上找到这个问题的解决方案,就像看起来那么简单。这是: 我有一个像df1这样的数据框:

                   PIM       WDR       MYC       OBX                           
ILMN_1651282  0.555764  0.675233 0.5908629 0.4897703                           
ILMN_1651354 0.6963458 0.8588675 0.9216328   0.88705                           
ILMN_1651358 0.7501548 0.6766059 0.8157319 0.9373666                           
ILMN_1652716 0.5505098 0.5802357 0.7342341 0.5953167                           
ILMN_1654324 0.9294231 0.9311051 0.8424824  0.888825                           
ILMN_1654639 0.9197155 0.4687101  0.678938 0.4309232                           
ILMN_1655418  0.690068 0.6345875 0.9595042 0.6132203

和像这样的数据框文件命名为df2:

            PIM WDR MYC OBX                                               
ILMN_1651282  -1  -1  -1  -1                                               
ILMN_1651354  -1   1   1   1                                               
ILMN_1651358   1   1   1   1                                               
ILMN_1652716  -1  -1  -1  -1                                               
ILMN_1654324  -1  -1  -1  -1                                               
ILMN_1654639  -1  -1  -1  -1                                               
ILMN_1655418   1   1  -1   1 

我的截止值为0.8。在df1中,每个高于0.8的值都在0中变化。所有低于0.8的值必须用df2(1& -1)中的值替换

创建了df2:

PIMvsEV<-list()
for (x in 1:nrow(df1)) {
t<-(if (mean(PIM[,x]) > mean(EV[,x])) {print(1)} else
if (mean(PIM[,x]) < mean(EV[,x])) {print(-1)} )
PIMvsEV[[x]]<-matrix(t)
}

WDRvsEV<-list()
for (x in 1:nrow(df1)) {
t<-(if (mean(WDR[,x]) > mean(EV[,x])) {print(1)} else
if (mean(WDR[,x]) < mean(EV[,x])) {print(-1)} )
WDRvsEV[[x]]<-matrix(t)
}

OBXvsEV<-list()
for (x in 1:nrow(cdf1)) {
t<-(if (mean(OBX[,x]) > mean(EV[,x])) {print(1)} else
if (mean(OBX[,x]) < mean(EV[,x])) {print(-1)} )
OBXvsEV[[x]]<-matrix(t)
}

MYCvsEV<-list()
for (x in 1:nrow(df1)) {
t<-(if (mean(MYC[,x]) > mean(EV[,x])) {print(1)} else
if (mean(MYC[,x]) < mean(EV[,x])) {print(-1)} )
MYCvsEV[[x]]<-matrix(t)
}

dataframe<-as.data.frame(cbind(as.matrix(PIMvsEV), as.matrix(WDRvsEV)))
dataframe<-as.data.frame(cbind(as.matrix(dataframe), as.matrix(MYCvsEV)))
dataframe<-as.data.frame(cbind(as.matrix(dataframe), as.matrix(OBXvsEV)))
row.names(dataframe)<-colnames(ttest)
colnames(dataframe)<-c("PIM","WDR","MYC","OBX")

有什么想法?非常感谢,

Lisanne

2 个答案:

答案 0 :(得分:1)

这应该做你想要的吗?

一些数据:

df1 <- as.data.frame(matrix(rnorm(100),10,10))
df2 <- matrix(sample(c(-1,1),100,T),10,10)

ifelse的矢量化使用:

 ifelse( as.matrix(df1) > 0.8, 0, df2)

      V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
 [1,]  0  0  1 -1  1  1 -1 -1  1  -1
 [2,] -1  1  1 -1 -1 -1 -1 -1  1  -1
 [3,] -1 -1 -1  0  0 -1  1  0  1  -1
 [4,]  0 -1 -1  1 -1  1 -1  1  1   1
 [5,] -1 -1 -1 -1  0  1 -1  1  0   0
 [6,] -1  1  1  1 -1  1  0  0  1   1
 [7,] -1  1  1  0 -1 -1 -1 -1 -1  -1
 [8,]  1  1  1  0 -1 -1  1 -1  0  -1
 [9,]  1  0  0  1  1  0 -1 -1  0  -1
[10,] -1 -1 -1 -1  1 -1 -1 -1 -1   1

或者我们可以做到

 (df1<= 0.8)*df2

答案 1 :(得分:1)

示例数据:

df1 <- as.data.frame(matrix(rnorm(100),10,10))
df2 <- as.data.frame(matrix(sample(c(-1,1),100,T),10,10))

data.frames代码

res <- df1 
res[df1>0.8] <- 0
res[df1<=0.8] <- df2[df1<=0.8] 
相关问题