Write a function that takes two arguments, x and n, and returns h(x, n). using FOR loop
Date : March 29 2020, 07:55 AM
wish of those help I am trying to write a function in R that takes two arguments, x and n, and returns h(x, n); x=1 Does anyone know how to do this using a for loop? h <- function( x, n ) sum( x^c(0:n) )
h( 1, 6 )
|
How to write an index/match function for dates and times that finds/returns the first value that is greater or less than
Tag : arrays , By : Killercode
Date : March 29 2020, 07:55 AM
around this issue I managed to find a solution to my problem! It's definitely not a very pretty solution but it works and gives me the output that I need. I thought I'd share what I found, just in case anyone is looking for a way to do this. However, I am sure there are better ways to do this, I just haven't figured any out. data$DTime <- as.POSIXct(data$DTime,format="%d/%m/%Y %H:%M")
d <- xts(data, order.by = data$DTime)
v <- xts(tempvar, order.by = tempvar$DTime)
var <- as.vector(v$Dtime)
data <- as.vector(d$Dtime)
"%GT%" <- function(x, y) min(which(x > y))
FirstValueGreaterThan <- as.vector(mode="integer",0)
for (i in 1:length(data)) {
FirstValueGreaterThan[i] <- var %GT% data[i]
}
variable.eg.temp.greaterThan <- tempvar[FirstValueGreaterThan,]
variable.eg.temp.greaterThan <- tempvar[FirstValueGreaterThan-1,]
data$newdata <- variable.eg.temp.greaterThan$temp or Dtime...
|
How to write a loop that locates a specific value in a column, if it finds one, write a formula to an adjacent cell, the
Tag : excel , By : Blight
Date : December 25 2020, 06:01 PM
Does that help I am trying to loop through a column looking for a specific value, if it finds that value, then write a formula in an adjacent cell, that happens to sum some values in another column. , A bit easier if you work from the bottom up: Sub Summarize()
Dim r As Long, rLast As Long, sht As Worksheet
Set sht = ActiveSheet
r = sht.Cells(Rows.Count, "B").End(xlUp).Row
rLast = r
Do
If sht.Cells(r, "C").Value = "Header" Then
sht.Cells(r, "D").Formula = "=SUM(B" & r & ":B" & rLast & ")"
rLast = r - 1
End If
r = r - 1
Loop While r > 1
End Sub
|
I need to write a VBA loop that finds a non-blank cell, then selects specific cells in that row to copy to another sheet
Tag : vba , By : Ian Badcoe
Date : March 29 2020, 07:55 AM
To fix the issue you can do Ok to after a bit of trial and error, I think I have managed to come up with a working solution. Feel free to check my code though! Sub Calculate()
Dim x As Long
Sheets("CPD").Select
Range("H7:N1449").Select
Selection.ClearContents
Sheets("Off the job training").Select
Range("H7:N1449").Select
Selection.ClearContents
For x = 1 To 50
Sheets("Record").Select
Range("C7").Offset(x - 1, 0).Select
If IsEmpty(ActiveCell) Then
Range("D7").Offset(x - 1, 0).Select
If IsEmpty(ActiveCell) Then
Range("E7").Offset(x - 1, 0).Select
If IsEmpty(ActiveCell) Then
Exit For
Else
Range(ActiveCell.Offset(0, -4), ActiveCell.Offset(0, -3)).Select
Selection.Copy
Sheets("CPD").Select
Range("H500").Select
Selection.End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Sheets("Off the job training").Select
Range("H500").Select
Selection.End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Sheets("Record").Select
Range("E7").Offset(x - 1, 0).Select
Selection.Copy
Sheets("CPD").Select
Range("H500").Select
Selection.End(xlUp).Offset(0, 2).Select
ActiveSheet.Paste
Sheets("Off the job training").Select
Range("H500").Select
Selection.End(xlUp).Offset(0, 2).Select
ActiveSheet.Paste
Sheets("Record").Select
Range("E7").Offset(x - 1, 0).Select
Range(ActiveCell(1, 2), ActiveCell(1, 5)).Select
Selection.Copy
Sheets("CPD").Select
Range("H500").Select
Selection.End(xlUp).Offset(0, 3).Select
ActiveSheet.Paste
Sheets("Off the job training").Select
Range("H500").Select
Selection.End(xlUp).Offset(0, 3).Select
ActiveSheet.Paste
End If
Else
Range(ActiveCell.Offset(0, -3), ActiveCell.Offset(0, -2)).Select
Selection.Copy
Sheets("Off the job training").Select
Range("H500").Select
Selection.End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Sheets("Record").Select
Range("D7").Offset(x - 1, 0).Select
Selection.Copy
Sheets("Off the job training").Select
Range("H500").Select
Selection.End(xlUp).Offset(0, 2).Select
ActiveSheet.Paste
Sheets("Record").Select
Range("D7").Offset(x - 1, 0).Select
Range(ActiveCell.Offset(0, 2), ActiveCell.Offset(0, 5)).Select
Selection.Copy
Sheets("Off the job training").Select
Range("H500").Select
Selection.End(xlUp).Offset(0, 3).Select
ActiveSheet.Paste
End If
Else
Range(ActiveCell.Offset(0, -2), ActiveCell.Offset(0, -1)).Select
Selection.Copy
Sheets("CPD").Select
Range("H500").Select
Selection.End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Sheets("Record").Select
Range("C7").Offset(x - 1, 0).Select
Selection.Copy
Sheets("CPD").Select
Range("H500").Select
Selection.End(xlUp).Offset(0, 2).Select
ActiveSheet.Paste
Sheets("Record").Select
Range("C7").Offset(x - 1, 0).Select
Range(ActiveCell.Offset(0, 3), ActiveCell.Offset(0, 6)).Select
Selection.Copy
Sheets("CPD").Select
Range("H500").Select
Selection.End(xlUp).Offset(0, 3).Select
ActiveSheet.Paste
End If
Next x
End Sub
|
Trying to Write a For Loop that finds "TRUE" in rows
Tag : r , By : Mare Astra
Date : March 29 2020, 07:55 AM
I wish this helpful for you If you want to store which rows are TRUE preallocate an empty vector and bind it in the loop. If you really have TRUE and FALSE as strings, do the following Holidays <- c("TRUE", "TRUE", "FALSE", "TRUE")
store = c()
for (i in 1:length(Holidays)){
if (Holidays[i] == "TRUE") # here you want to see whether the element inside Holiday is equals "TRUE"
store = c(store, i) # store vector you give you which rows are "TRUE"
}
Holidays <- c(TRUE, TRUE, FALSE, TRUE)
store = c()
for (i in 1:length(Holidays)){
if (Holidays[i] == TRUE)
store = c(store, i)
}
store = which(Holidays %in% TRUE)
|