r - Using an if statement in apply in R for every value in a data frame
问题描述
I have a data frame that I created using the read_excel
function and then duplicated it. I'm going to explain it as if I was using Excel, because it's so easy to do this in Excel. I want to check if each cell in each row within columns 3 to 11 have a zero, and if so, put a zero in columns 12 to 20. If not, keep the original value.
Data2 <- Data1
Data2[,12:20] <- apply(Data1[,3:11],1:2,function(x) {if(x==0) {0})
This is the error message I get:
Warning message: In
[<-.data.frame
(*tmp*
, , 12:20, value = list(0, 0, 0, 0, 0, : provided 450 variables to replace 9 variables
Example:
Data1 <- matrix(data=c(0,1,1,0,3,4,5,6,2,3,0,5,6,5,6,2,6,2,3,4,5,6,5,6),nrow=6,ncol=4)
Data2 <- Data1
Data2[,3:4] <- apply(Data1[,1:2],1:2,function(x) if(x==0) {0})
Data2 <- matrix(Data2,nrow=6,ncol=4)
The result should look like this:
[,1] [,2] [,3] [,4]
[1,] 0 5 0 3
[2,] 1 6 5 4
[3,] 1 2 6 5
[4,] 0 3 0 6
[5,] 3 0 6 0
[6,] 4 5 2 6
where any zero in columns 1 and 2 become zeros in the appropriate spot in columns 3 and 4.
Instead, I get this:
[,1] [,2] [,3] [,4]
[1,] 0 5 0 NULL
[2,] 1 6 NULL NULL
[3,] 1 2 NULL NULL
[4,] 0 3 0 NULL
[5,] 3 0 NULL 0
[6,] 4 5 NULL NULL
Also, I'm still getting the same error message from the original data that had 50+ row and 20 columns shown at the beginning.
解决方案
这是一个替代解决方案:
首先,创建一个逻辑矩阵,表示感兴趣的列中哪些元素为 0。
mat <- Data1[,1:2] == 0
mat
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE FALSE
[3,] FALSE FALSE
[4,] TRUE FALSE
[5,] FALSE TRUE
[6,] FALSE FALSE
然后,选择逻辑矩阵具有TRUE
值的目标列的元素并将其设置为 0:
Data2[,3:4][mat==TRUE] <- 0
Data2
[,1] [,2] [,3] [,4]
[1,] 0 5 0 3
[2,] 1 6 5 4
[3,] 1 2 6 5
[4,] 0 3 0 6
[5,] 3 0 6 0
[6,] 4 5 2 6
推荐阅读
- angular - 表单验证错误消息以角度提交时显示
- java - 如何从字符串资源文件中隐藏第一项并在微调器中显示
- php - 三个mysql触发器
- c# - 检索 COM 类工厂失败错误:80040154
- ios - 带有 UIBackgroundFetchResult 的 didReceiveRemoteNotification 在 IOS 中不起作用
- scala - 作为执行器和线程数量的函数,火花中的良好分区数量是多少?
- javafx - 树莓派挂在谷歌日历 api 的 oauth 身份验证中
- wordpress - 关于 Wordpress DIVI 布局设置的问题
- css - React DatePicker 日历显示在表头后面
- sql - AND/OR 运算符未按需要过滤