R 中 Data.Frame 中的逻辑运算符



这是mydataframe:

x<-structure(list(ABEV3 = c(1, -0.0329694340790399, -0.600795689797522, 
-0.300219759273298), AZUL4 = c(-0.0329694340790399, 1, 0.531987810491871, 
0.884036954118666), BTOW3 = c(-0.600795689797522, 0.531987810491871, 
1, 0.612824308045668), B3SA3 = c(-0.300219759273298, 0.884036954118666, 
0.612824308045668, 1)), row.names = c("ABEV3", "AZUL4", "BTOW3", 
"B3SA3"), class = "data.frame")

我想删除所有符合此条件的值:如果 x[i,j] 大于 0.85 且小于 0.99,它将保留在我的数据帧中(保留行和列名称):

所以我这样做了:

y<- x>0.35 & x<0.99

但是当我运行时:x[y]它不是数据帧。并且它不保留列/行名称。

如何使用相同的步骤更正此问题?即,使用逻辑矩阵 y ?

我的最终输出应该是这个:

out<-structure(c(0.5319878, 0.884037, 0.53198781, 0.88403695, 0.6128243, 
0.5319878), .Dim = 3:2, .Dimnames = list(c("AZUL4", "BTOW3", 
"B3SA3"), c("BTOW3", "B3SA3")))

PS:这个想法只是从我的 x data.frame 中删除与x>0.85 & x<0.99条件不匹配的值。

不能只删除值而不删除整行或整列。我想你想要这个——

x[x > 0.85 & x < 0.99] <- NA_real_
ABEV3       AZUL4       BTOW3      B3SA3      BRSR6      BBSE3        BRML3      BBDC3      BBDC4        BRAP4
ABEV3  1.00000000 -0.03296943 -0.60079569 -0.3002198 -0.3971484 -0.1111854 -0.201374719 -0.1459591 -0.2943246 -0.048831117
AZUL4 -0.03296943  1.00000000  0.53198781         NA         NA         NA  0.830385751         NA         NA  0.214403356
BTOW3 -0.60079569  0.53198781  1.00000000  0.6128243  0.8196140  0.4432318  0.560694380  0.6530405  0.7307261  0.064940995
B3SA3 -0.30021976          NA  0.61282431  1.0000000         NA         NA  0.820488340         NA         NA  0.184650344
BRSR6 -0.39714843          NA  0.81961398         NA  1.0000000  0.7963561  0.781602231         NA         NA  0.140952812
BBSE3 -0.11118541          NA  0.44323184         NA  0.7963561  1.0000000           NA         NA         NA  0.136343833
BRML3 -0.20137472  0.83038575  0.56069438  0.8204883  0.7816022         NA  1.000000000         NA         NA  0.009409914
BBDC3 -0.14595908          NA  0.65304052         NA         NA         NA           NA  1.0000000         NA  0.116203752
BBDC4 -0.29432460          NA  0.73072606         NA         NA         NA           NA         NA  1.0000000  0.126570544
BRAP4 -0.04883112  0.21440336  0.06494099  0.1846503  0.1409528  0.1363438  0.009409914  0.1162038  0.1265705  1.000000000

您可以使用dplyr尝试此方法...

x <- x %>% mutate(r=rownames(.)) %>% gather(k,v,-r,factor_key = T) %>% 
filter(v>0.85 & v<0.99) %>% spread(k,v)
rownames(x) <- x$r
x <- x %>% select(-r)
x
AZUL4     B3SA3     BRSR6     BBSE3     BRML3     BBDC3     BBDC4
AZUL4        NA 0.8840370 0.8608257 0.8861292        NA 0.9185312 0.8885266
B3SA3 0.8840370        NA 0.9109177 0.9144024        NA 0.9193643 0.9450005
BBDC3 0.9185312 0.9193643 0.9171293 0.9163145 0.9043771        NA 0.9844133
BBDC4 0.8885266 0.9450005 0.9571348 0.8981849 0.8848416 0.9844133        NA
BBSE3 0.8861292 0.9144024        NA        NA 0.8980725 0.9163145 0.8981849
BRML3        NA        NA        NA 0.8980725        NA 0.9043771 0.8848416
BRSR6 0.8608257 0.9109177        NA        NA        NA 0.9171293 0.9571348

这是否提供了所需的输出?它将删除没有满足条件的值的列/行,即 ABEV3 被完全删除并且不返回。

相关内容

  • 没有找到相关文章

最新更新