sum multiple which statements with loop? - r

I'm computing a new variable in a large df in r. I wrote some code that fills my empty cells:
data2$departure_time[which(data2$No=="3" & data2$Key=="300111")]<-data2$departure_time[which(data2$No== "2" & data2$Key=="300111")] + data2$travel_time[which(data2$No=="2" & data2$Key=="300111")] + data2$waiting_time[which(data2$o=="2" & data2$Key=="300111")]
this works so far. yet i need to adapt No-statement in the code every time. What needs to be done is just add 1 to each No statement.
So the result would be:
data2$departure_time[which(data2$No=="4" & data2$Key=="300111")]<-data2$departure_time[which(data2$No== "3" & data2$Key=="300111")] + data2$travel_time[which(data2$No=="3" & data2$Key=="300111")] + data2$waiting_time[which(data2$o=="3" & data2$Key=="300111")]
And so on..
I tried to implement a for-loop into my code as I thougt I would be the easiest solution. So I tried the following:
for(in in 3:40){data2$departure_time[which(data2$No=i & data2$Key=="300111")]<-for(j in 2:40){data2$departure_time[which(data2$No= j & data2$Key=="300111")] + data2$travel_time[which(data2$No= j & data2$Key=="300111")] + data2$waiting_time[which(data2$No=j & data2$Key=="300111")]}}
Unfortunately this didn't work out. I never wrote a loop in R and don't know how to solve this otherwise.
Thank you in advance!

Related

How to write multiple if/else if conditions in R on Row record selection

I have a simple question on adding a Flag to indicate if the day is out of the scheduled range. As shown in the following image, each should be occurred within a 6 day range, e.g., for Week 2, the should be 9 <= STDTY <= 21, otherwise it will be flagged as Flag="Y".
if (data$VISIT=="Screening" & data$STDTY>=-1) {
data$Flag="Y"
} else if (data$VISIT=="Day 1" & data$STDTY!=1) {
sv_domain$Flag="Y"
} else if (data$VISIT=="Week 2" & data$STDTY<(2*7+1-6)) {
data$Flag="Y"
} else if (data$VISIT=="Week 2" & data$STDTY>(2*7+1+6)) {
data$Flag="Y"
.......
I know it doesn't work, please help me out, thanks!
if/else is not vectorized. We may use ifelse or more easily with case_when
library(dplyr)
case_when(data$VISIT=="Screening" & data$STDTY>=-1|
data$VISIT=="Week 2" & data$STDTY<(2*7+1-6)|
data$VISIT=="Week 2" & data$STDTY>(2*7+1+6) ~ "Y")
Or with ifelse
ifelse(data$VISIT=="Screening" & data$STDTY>=-1|
data$VISIT=="Week 2" & data$STDTY<(2*7+1-6)|
data$VISIT=="Week 2" & data$STDTY>(2*7+1+6), "Y", NA)

replacing part of a string in a boolean rule with indices

i need to replace this part X[,...] in this string with something else
r <- "X[,1]<=-0.00595 & X[,2]<=-0.00605 & X[,20]>-0.00625 & X[,25]>-0.00615"
I would like to get something like this
X[,...] replase to Q
r <- "Q<=-0.00595 & Q<=-0.00605 & Q>-0.00625 & Q>-0.00615"
Here is one possible solution:
r <- "X[,1]<=-0.00595 & X[,2]<=-0.00605 & X[,20]>-0.00625 & X[,25]>-0.00615"
gsub("X\\[,\\d*\\]", "Q", r)
# "Q<=-0.00595 & Q<=-0.00605 & Q>-0.00625 & Q>-0.00615"

For/While Loop on variables that satisfy a certain condition [R]

urI'm trying to write a if else statement (ultimately) in R, but only for variables that satisfy a certain criteria. I'm sure there is an easy way to do this - but can't seem to find anything specific when searching...
Below is an example of a while loop (not sure whether I can use this for this purpose):
while(gene[c(36)] >=30 & gene[c(37)] >=30 & gene[c(38)] >=30)
{
gene$Category <- ifelse((gene[c(49)] == './.' & gene[c(48)] == './.'), 'N/A', ifelse(((gene[c(50)] == './.') & (gene[c(36)] >=30 & gene[c(37)] >=30)),'denovo deletion',''))
}
I technically want to run the if else statement on a variable(s) only if certain other conditions are met. Am I overly complicating this?
Assuming that your ifelse construct is OK, you can "subset" the frame based on the condition that is now expressed in your while loop:
condition = (gene[36] >=30 & gene[37] >=30 & gene[38] >=30)
gene$Category[condition] <- ifelse((gene[49] == './.' & gene[48] == './.'), 'N/A', ifelse(((gene[50] == './.') & (gene[36] >=30 & gene[37] >=30)),'denovo deletion',''))

Create new data set that meets all of 4 conditions

I would like to create a new dataset where the following four conditions are all met.
rowSums(is.na(UNCA[,11:23]))<12
rowSums(is.na(UNCA[,27:39]))<12
rowSums(is.na(UNCA[,40:52]))<12
rowSums(is.na(UNCA[,53:65]))<12
Thanks!
Then use the & operator:
UNCA.new <- UNCA[rowSums(is.na(UNCA[,11:23])) < 12 &
rowSums(is.na(UNCA[,27:39])) < 12 &
rowSums(is.na(UNCA[,40:52])) < 12 &
rowSums(is.na(UNCA[,53:65])) < 12, ]
A single & is a vectorized function, while a double && is unary (typically used in an if statement, for instance).

How to do the square root in PowerPoint VBA?

Here is some math code that adds A to B:
Sub math()
A = 23
B = 2
ABSumTotal = A + B
strMsg = "The answer is " & "$" & ABSumTotal & "."
MsgBox strMsg
End Sub
But how can I calculate a square root of ABSumTotal?
Is it possible in PowerPoint VBA?
Use: Sqr()
strMsg = "The answer is " & "$" & Sqr(ABSumTotal) & "."
Have you tried the function Sqr?
See:
http://msdn.microsoft.com/en-us/library/h2h9y284%28VS.85%29.aspx
You can use use ^ to compute X^(1/2)
Edit: added parenthesis

Resources