R将ifelse语句应用于data.frame的每个单元格

时间:2017-01-22 20:54:35

标签: r dataframe apply

我编辑了这个问题(希望按照要求)

我需要检查data.frame的每个单元格,如果它的值在某个范围内。我很新,申请并需要努力理解它。

我有2个data.frames:

  • blood_df:158列,

  • stat_df:每个blood_df

  • 的统计信息

附件是解释的最小例子:

到目前为止,我得到了这个,但它为每个细胞计算相同的结果。

c0 <- c(0,0,0,0)
c1 <- c(1,2,3,4)
c2 <- c(5,6,7,8)
c3 <- c(9,10,11,12)
c4 <- c(13,14,15,16)

blood_df <- data.frame(c0,c1,c2,c3,c4)
stat_df <- data.frame(matrix(ncol = 5, nrow = 6))
colnames(stat_df) <- colnames(blood_df)
rownames(stat_df) <- c("Mean","3*sd","sum", "Mean2","-3*sd","sum2" )

stat_df[1,2:5] <-apply(blood_df[,2:5], 2,  mean, na.rm = TRUE)
stat_df[2,2:5] <-apply(blood_df[1:4,2:5], 2, function(x)  3*sd(x,na.rm=TRUE))
stat_df[3,] <-colSums(stat_df[1:2,])
stat_df[4,2:5] <-apply(blood_df[,2:5], 2,  mean, na.rm = TRUE)
stat_df[5,2:5] <-apply(blood_df[1:4,2:5], 2, function(x) -3*sd(x,na.rm=TRUE))
stat_df[6,] <-colSums(stat_df[4:5,])

blood_df:
##   c0 c1 c2 c3 c4
## 1  0  1  5  9 13
## 2  0  2  6 10 14
## 3  0  3  7 11 15
## 4  0  4  8 12 16

stat_df:
##       c0        c1        c2        c3        c4
## Mean  NA  2.500000  6.500000 10.500000 14.500000
## 3*sd  NA  3.872983  3.872983  3.872983  3.872983
## sum   NA  6.372983 10.372983 14.372983 18.372983
## Mean2 NA  2.500000  6.500000 10.500000 14.500000
## -3*sd NA -3.872983 -3.872983 -3.872983 -3.872983
## sum2  NA -1.372983  2.627017  6.627017 10.627017 

因我需要而无效的部分:

blood_df[1:4,2:5] <- apply(blood_df[,2:5],2,  function(x) 
                   (ifelse((x > (stat_df[3,2:5]))|| 
                   (x < (stat_df[6,2:5])), NA, x)))

到目前为止它给了我:

blood_df:
##   c0 c1 c2 c3 c4
## 1  0  1  1  1  1
## 2  0  5  5  5  5
## 3  0 NA NA NA NA
## 4  0 NA NA NA NA

我想得到的是:(检查每个值是否在一定范围之间)

blood_df:
##   c0 c1 c2 c3 c4
## 1  0  1  5  9 13
## 2  0  2  6 10 14
## 3  0  3  7 11 15
## 4  0  4  8 12 16

如果它不在范围内,则该值应更改为NA。

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试Sub LoanSchedule() Dim intRate, loanLife, initLoan, payment As Double Dim yearBegBal, intComp, prinComp, yearEndBal, intTot, prinTot, fvloan As Currency ActiveSheet.UsedRange.Delete intRateYrs = InputBox("Input Interest rate (Annual):") loanLifeYrs = InputBox("Input Loan life (Years):") initLoan = InputBox("Input Loan amount:") Application.DisplayAlerts = True Application.ScreenUpdating = True intRateMths = (intRateYrs / 100) / 12 loanLifeMths = loanLifeYrs * 12 Cells(4, 2).Value = Format(intRateYrs, "#.##") & " %" Cells(4, 3).Value = Format(intRateMths, "Percent") Cells(5, 2).Value = loanLifeYrs Cells(5, 3).Value = loanLifeMths Cells(6, 2).Value = Format(initLoan, "Currency") payment = Pmt(intRateMths, loanLifeMths, -initLoan) Cells(7, 2).Value = Format(payment, "Currency") outRow = 10 intTot = 0 prinTot = 0 fvloan = 0 Cells(10, 2).Value = "Beginning Balance" Cells(10, 3).Value = "Payment" Cells(10, 4).Value = "Interest" Cells(10, 5).Value = "Principal" Cells(10, 6).Value = "End Balance" Cells(10, 7).Value = "Total Interest" Cells(10, 8).Value = "Total Principal" Cells(10, 9).Value = "Total Repaid" yearBegBal = initLoan For rowNum = 1 To loanLifeMths intComp = yearBegBal * intRateMths prinComp = payment - intComp yearEndBal = yearBegBal - prinComp intTot = intTot + intComp prinTot = prinTot + prinComp fvloan = intTot + prinTot Cells(outRow + rowNum, 1).Value = rowNum Cells(outRow + rowNum, 2).Value = Format(yearBegBal, "Currency") Cells(outRow + rowNum, 3).Value = Format(payment, "Currency") Cells(outRow + rowNum, 4).Value = Format(intComp, "Currency") Cells(outRow + rowNum, 5).Value = Format(prinComp, "Currency") Cells(outRow + rowNum, 6).Value = Format(yearEndBal, "Currency") Cells(outRow + rowNum, 7).Value = Format(intTot, "Currency") Cells(outRow + rowNum, 8).Value = Format(prinTot, "Currency") Cells(outRow + rowNum, 9).Value = Format(fvloan, "Currency") yearBegBal = yearEndBal Next rowNum ActiveSheet.Range("A:I").EntireColumn.AutoFit Rows("11:11").Select ActiveWindow.FreezePanes = True Range("A1").Select Application.DisplayAlerts = False Application.ScreenUpdating = False End Sub

mapply