- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个大矩阵,约 300 行和 200000 列。我想通过选择至少有一个值大于 0.5 或小于 -0.5(不仅仅是那个特定值)的整个列来缩小这一范围。我想保留行名和列名。通过执行 tmp<-mymat > 0.5 | mymat < -0.5
,我能够得到真假矩阵。 .我想提取所有至少有一个 TRUE
的列在他们中。我简单地尝试了 mymat[tmp]
但这只会返回满足该条件的值的向量。如何获得原始矩阵的实际列?谢谢。
最佳答案
试试这个:
> set.seed(007) # for the example being reproducible
> X <- matrix(rnorm(100), 20) # generating some data
> X <- cbind(X, runif(20, max=.48)) # generating a column with all values < 0.5
> colnames(X) <- paste('col', 1:ncol(X), sep='') # some column names
> X # this is how the matrix looks like
col1 col2 col3 col4 col5 col6
[1,] 2.287247161 0.83975036 1.218550535 0.07637147 0.342585350 0.335107187
[2,] -1.196771682 0.70534183 -0.699317079 0.15915528 0.004248236 0.419502015
[3,] -0.694292510 1.30596472 -0.285432752 0.54367418 0.029219842 0.346358090
[4,] -0.412292951 -1.38799622 -1.311552673 0.70480735 -0.393423429 0.212185020
[5,] -0.970673341 1.27291686 -0.391012431 0.31896914 -0.792704563 0.224824248
[6,] -0.947279945 0.18419277 -0.401526613 1.10924979 -0.311701865 0.415837389
[7,] 0.748139340 0.75227990 1.350517581 0.76915419 -0.346068592 0.057660111
[8,] -0.116955226 0.59174505 0.591190027 1.15347367 -0.304607588 0.007812921
[9,] 0.152657626 -0.98305260 0.100525456 1.26068350 -1.785893487 0.298192099
[10,] 2.189978107 -0.27606396 0.931071996 0.70062351 0.587274672 0.216225091
[11,] 0.356986230 -0.87085102 -0.262742349 0.43262716 1.635794434 0.026097800
[12,] 2.716751783 0.71871055 -0.007668105 -0.92260172 -0.645423474 0.190567072
[13,] 2.281451926 0.11065288 0.367153007 -0.61558421 0.618992169 0.402829397
[14,] 0.324020540 -0.07846677 1.707162545 -0.86665969 0.236393598 0.248196976
[15,] 1.896067067 -0.42049046 0.723740263 -1.63951709 0.846500899 0.406511129
[16,] 0.467680511 -0.56212588 0.481036049 -1.32583924 -0.573645739 0.162457572
[17,] -0.893800723 0.99751344 -1.567868244 -0.88903673 1.117993204 0.383801555
[18,] -0.307328300 -1.10513006 0.318250283 -0.55760233 -1.540001132 0.347037954
[19,] -0.004822422 -0.14228783 0.165991451 -0.06240231 -0.438123899 0.262938992
[20,] 0.988164149 0.31499490 -0.899907630 2.42269298 -0.150672971 0.139233120
>
> # defining a index for selecting if the condition is met
> ind <- apply(X, 2, function(X) any(abs(X)>0.5))
> X[,ind] # since col6 only has values less than 0.5 it is not taken
col1 col2 col3 col4 col5
[1,] 2.287247161 0.83975036 1.218550535 0.07637147 0.342585350
[2,] -1.196771682 0.70534183 -0.699317079 0.15915528 0.004248236
[3,] -0.694292510 1.30596472 -0.285432752 0.54367418 0.029219842
[4,] -0.412292951 -1.38799622 -1.311552673 0.70480735 -0.393423429
[5,] -0.970673341 1.27291686 -0.391012431 0.31896914 -0.792704563
[6,] -0.947279945 0.18419277 -0.401526613 1.10924979 -0.311701865
[7,] 0.748139340 0.75227990 1.350517581 0.76915419 -0.346068592
[8,] -0.116955226 0.59174505 0.591190027 1.15347367 -0.304607588
[9,] 0.152657626 -0.98305260 0.100525456 1.26068350 -1.785893487
[10,] 2.189978107 -0.27606396 0.931071996 0.70062351 0.587274672
[11,] 0.356986230 -0.87085102 -0.262742349 0.43262716 1.635794434
[12,] 2.716751783 0.71871055 -0.007668105 -0.92260172 -0.645423474
[13,] 2.281451926 0.11065288 0.367153007 -0.61558421 0.618992169
[14,] 0.324020540 -0.07846677 1.707162545 -0.86665969 0.236393598
[15,] 1.896067067 -0.42049046 0.723740263 -1.63951709 0.846500899
[16,] 0.467680511 -0.56212588 0.481036049 -1.32583924 -0.573645739
[17,] -0.893800723 0.99751344 -1.567868244 -0.88903673 1.117993204
[18,] -0.307328300 -1.10513006 0.318250283 -0.55760233 -1.540001132
[19,] -0.004822422 -0.14228783 0.165991451 -0.06240231 -0.438123899
[20,] 0.988164149 0.31499490 -0.899907630 2.42269298 -0.150672971
# It could be done just in one step avoiding 'ind'
X[, apply(X, 2, function(X) any(abs(X)>0.5))]
关于R选择至少一个值满足条件的整个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11652879/
我想检索具有多个条件的数据,其中每个条件将在特定字段中包含特定关键字。 表结构如下: sid nid cid 数据 50 7 5 ee 50 7 6 AA 50 7 8 ff 51 7 5 ee 51
在 Prolog 中,我经常通过提供模板(包含变量的结构)然后满足其上的一组约束来解决问题。一个简单的例子可能是: go(T) :- T = [_, _, _], member(cat
在设计 FPGA 系统时,我如何粗略估计给定任务所需的逻辑 block 数量? 有人对我对这些常见设备的期望有一个粗略的数量级吗? 串口 使用 CRC32 的数据包解帧器 8 微核 我看过 www.o
我需要编写一段代码,如果函数满足列表中的大多数元素,则返回 True,不满足其中的 false。例如:moreThan odd [1,2,3] 是 True,但是 moreThan odd [1,2,
一旦满足三个条件,我需要使用 componentWillReceiveProps() 来调用我的组件中的方法。其中两个条件将当前 Prop 与下一个 Prop 进行比较,这两个条件通过 Ajax 请求
我正在构建一个主从表单。主视图模型构造细节 View 模型的实例。这些细节 View 模型有几个依赖项,需要用新 类实例来满足。 (这是因为他们需要在独立于主虚拟机的数据上下文中运行的服务层。) 实现
我有以下项目,我已经使用了一段时间。正如您在运行 snnipets 后看到的那样,一切正常。 /* The dark background behind the dialogs */ .dialog-
我正在尝试找出解决此问题的方法: 我想要一个函数来检查文本字段是否填充了文本并且复选框是否被选中。当满足这些条件时,“提交”按钮将启用。如果启用“提交”按钮后不久,用户清除文本字段或取消选中复选框,则
所以我相对较新,我有以下代码,我想知道如何制作这样我可以返回临时变量,同时满足java的返回要求。我希望返回临时值,但由于它位于 if-else block 内,因此从技术上讲,它不会在其外部初始化。
我正在编写一个脚本,该脚本读取文本文件并根据 .txt 文件的内容更改 div 中的文本。 但这不是我的问题。我不想要纯文本,背景颜色应该根据满足 if/elseif/else 函数的条件而改变。 v
我想在 if let 构造中满足多个约束。我知道我们可以使用“,”(逗号)来解包多个值,但它们都必须成功解包。 例如: var str: String? = "Hello" var x: Int? =
当我在 genymotion 模拟设备上安装我的应用程序时,它无法很好地安装,在控制台上我得到“INSTALL_FAILED_CPU_ABI_INCOMPATIBLE”我尝试了另一个应用程序,它安装得
因此,我试图根据数据帧的匹配条件来查看数据帧的两个变量(v1 和 v2)是否在其符号(正数或负数)中匹配变量(ID1==ID2)。 示例数据框 - Trial.df: ID1 v1
如果交付一个 Java 应用程序,它使用 gradle 依赖管理和许多来自 maven-central 的开源库,是否足以检查第一级 depedencies 的许可证(因为他们的依赖关系必须再次自动与
我正在尝试创建一个满足接口(interface) Iterable 的类“Gprogram” (这样我就可以在我的 Gprogram 中迭代 Gcommand)。但是,我只能使用类型 Iterable
我想知道是否可以获得一些帮助。 我试图在查询中写入一个查询,我使用 3 个字段:ID、选项和金额。 我需要对我的唯一 ID 进行分组,然后在该组中我需要按选项白色进行拆分,总计每个选项的金额。例如:编
如何在iOS swift项目中配置Jitsi-meet框架开启视频通话服务? 最佳答案 编辑:这也适用于 Xcode Version 12.2 (12B45b)在 Mac OS Big Sur 上。
我正在玩一些交互式菜单,目前有一个隐藏菜单,当按下一个按钮时,它会从右边出现,并将整个内容移到上面。有点像移动 facebook 应用程序。为了确定按钮应该将菜单滑出还是放回我使用 javascrip
我的目标很简单,使用遗传算法重现经典的“Hello, World”字符串。 我的代码基于此 post .代码主要包含4个部分: 生成具有多个不同个体的种群 根据与target的比较,定义评估个体好坏的
问题陈述 我们有一个雇主想要面试 N 个人,因此安排了 N 个面试时段。每个人都有这些时段的忙闲时间表。给出一个算法,如果可能的话将 N 个人安排到 N 个槽位,如果不可能则返回一个标志/错误/等。最
我是一名优秀的程序员,十分优秀!