Question
I am new to programming and am experiencing an issue with conditional logic. In my function the condition has length > 1 and thus only the first case of it being true will populate. How would I go about implementing the correct functionality?
Output
I get incorrect information for the BaseQoQ and StressQoQ calculations for all regions with counter = 1. How would I modify my code so the correct information populates for a conditional with length > 1? I am new to R so any insight is much appreciated.
The vectorised form of ifcondition in R is ifelse, whose definition can be found here. In your example one can have
with(HPF,
ifelse(index != 0,
case1,
case2)
)
notice that you may want to wrap case1 (respectively case2) assignments into independent functions, to avoid confusions with the ifelse commas themselves.
Related
So I kind of already know the possible solution but I don't know how to exactly go about it so please give me a bit of grace here.
I have a dataset for youtube trends that I want to read the values from two columns (likes and dislikes) and based off their contents I want an entry to be made in the new column. If the likes are higher than the dislikes I want it to be said as a 'positive' video and if it has more dislikes it should be 'negative'.
I'm primarily not sure how to go about this since most of the previous asks are based off of one column rather than two. I know some mentioned using cut, but would it still work the same?
all help is appreciated, thanks.
You can use a simple ifelse :
df$new_col <- ifelse(df$likes > df$dislikes, 'positive', 'negative')
This can also be written without ifelse as :
df$new_col <- c('negative', 'positive')[as.integer(df$likes > df$dislikes) + 1]
You can use Vectorize to create a vectorized version of a function. vfunc <- Vectorize(func) will allow you to call df$newcol <- vfunc(df$likes, df$dislikes) if your function takes two arguments and then return the result for each row in a vector that's assigned to a new column.
First of all, I have to say that this is my first post. Despite of having look for the answer using the search toolbox it might be possible that I passed over the right topic without realizing myself, so just in case sorry for that.
Having said that, my problem is the following one:
I have a data table composed by several columns.
I have to select the
rows that are fullfilling one specific condition ex.
which(DT_$var>value, arr.ind = T)) or which(DT_$var>value &&
DT_$var2>value2, arr.ind = T))
I have to keep these columns in a new
data frame.
My approach was the following one but it is not working, probably because I did not understand the loops correctly:
while (i in nrow(DT)) {
if(DT$var[i]>value){
DT_aux[i]=DT[i]
i<-i+1
}
}
Error in if (DT$value[i] > 45) { : argument is of length zero
I hope that you can help me
There is a very good chance that you want to use dplyr and it's filter function. It would work like this:
library(dplyr)
DT %>% filter(var>value && var2>value2)
You don't need to use DT$var and DT$var2 here; dplyr knows what you mean when you refer to variables.
You can, of course, do the same with base R, but this kind of work is exactly what dplyr was made for, so sticking with base R, in this case, is just masochism.
I did not find an answer to this - in my opinion quite basic - question. So in case I missed out on an already existing solution, I am sorry for that and would appreciate a link to the thread.
I am facing the following problem:
I want to create an if-condition whether or not an observations fulfills certain criteria. However, the set of variables i want to test is unknown, as they are created in the process and might change, depending on the data fed into the model.
I now have hard-coded the variable names, like below:
data$selectvar <- ifelse(data$crit1 == 1 | data$crit2 == 1 | data$crit3 == 1, 1, 0)
In above example, there could be cases where e.g., I only have crit1 and crit3 in the data set data, but not crit2. So the if condition would throw an error in these cases.
The way I have named the variables is that they all have the same prefix, so maybe there is a way to work with grepl or similar, but I don't know how.
There are many ways to do it, but if you want to use ifelse then you can try this..
ifelse(data[,grep("crit",colnames(data))]==1,1,0)
I am new to TOSCA, kindly guide me How to use loop statements and condition statements in TOSCA...
Thanks in advance.
Sreeni
The best way to find help on this is to refer Tosca documentation.
Please check this : https://support.tricentis.com/community/manuals_detail.do?lang=en&version=10.0.0&url=tchb/conditional_statements.htm
The above link refers to the manual for Tosca 10.0
You can use IF statement for condition, in which you can compare values within tbox set buffer and choose the direction with Then or Else statement.
For looping there are two options While and Do-While loops. Syntax remains same like other programming languages, only thing need to take care is that you need to mention repetition count for looping. By default repetition count for loop is 30, which can be changed to any count you want, just it gives false error when the count has reached to maximum if the loop doesn't end at 'n-1' count.
To avoid this false error you can first take count of elements on which actually you want to apply the loop and then set that value to repetition count by passing a buffer value.
I'm trying to subset a dataframe within a function using a mixture of fixed variables and some variables which are created within the function (I only know the variable names, but cannot vectorise them beforehand). Here is a simplified example:
a<-c(1,2,3,4)
b<-c(2,2,3,5)
c<-c(1,1,2,2)
D<-data.frame(a,b,c)
subbing<-function(Data,GroupVar,condition){
g=Data$c+3
h=Data$c+1
NewD<-data.frame(a,b,g,h)
subset(NewD,select=c(a,b,GroupVar),GroupVar%in%condition)
}
Keep in mind that in my application I cannot compute g and h outside of the function. Sometimes I'll want to make a selection according to the values of h (as above) and other times I'll want to use g. There's also the possibility I may want to use both, but even just being able to subset using 1 would be great.
subbing(D,GroupVar=h,condition=5)
This returns an error saying that the object h cannot be found. I've tried to amend subset using as.formula and all sorts of things but I've failed every single time.
Besides the ease of the function there is a further reason why I'd like to use subset.
In the function I'm actually working on I use subset twice. The first time it's the simple subset function. It's just been pointed out below that another blog explored how it's probably best to use the good old data[colnames()=="g",]. Thanks for the suggestion, I'll have a go.
There is however another issue. I also use subset (or rather a variation) in my function because I'm dealing with several complex design surveys (see package survey), so subset.survey.design allows you to get the right variance estimation for subgroups. If I selected my group using [] I would get the wrong s.e. for my parameters, so I guess this is quite an important issue.
Thank you
It's happening right as the function is trying to define GroupVar in the beginning. R is looking for the object h by itself (not within the dataframe).
The best thing to do is refer to the column names in quotes in the subset function. But of course, then you'd have to sidestep the condition part:
subbing <- function(Data, GroupVar, condition) {
....
DF <- subset(Data, select=c("a","b", GroupVar))
DF <- DF[DF[,3] %in% condition,]
}
That will do the trick, although it can be annoying to have one data frame indexing inside another.