Error saying "EXPR must be a length 1 vector" when trying to use the switch functionality

0 votes

In R programming I try to do the following:

    df
    A       B     Category
    0.9     0.85  A
    0.7     0.75  B
    0.8     0.90  B

    CSF <- function(df, type) {
    switch(type,
               "A" = qnorm(df$A, 0 , 1),
               "B" = qnorm(df$B, 0 , 1)
    )
    }

    df<-data.frame(df, value = CSF(df,df$category))

Desired result:

    df
    A       B     Category     Value
    0.9     0.85  A            qnorm(0.9, 0, 1)*
    0.7     0.75  B            qnorm(0.75, 0, 1)*
    0.8     0.90  B            qnorm(0.9, 0, 1)*

*: real values

Error message: EXPR must be a length 1 vector

Nov 16, 2018 in Data Analytics by Ali
• 10,430 points
496 views

1 answer to this question.

0 votes

You can use the ifelse function:

df$Value <- ifelse(df$Category=="A",qnorm(df$A,0,1),qnorm(df$B,0,1))
answered Nov 16, 2018 by Maverick
• 10,040 points

Related Questions In Data Analytics

0 votes
1 answer

Error saying cannot open file 'file.pdf' when trying to save a plot in ggplot

You could try adding the following line ...READ MORE

answered Nov 9, 2018 in Data Analytics by Maverick
• 10,040 points
506 views
0 votes
1 answer

Error saying "duplicate 'row.names' are not allowed" when trying to setup my data for the mlogit-package

Take out the chid.var argument in your call to mlogit.data, ...READ MORE

answered Nov 12, 2018 in Data Analytics by Maverick
• 10,040 points
291 views
0 votes
1 answer

Error saying "Error in df$item : object of type 'closure' is not subsettable" when trying to use arules package

Try replacing ID <- c("A123","A123","A123","A123","B456","B456","B456") item <- c("bread", "butter", ...READ MORE

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,040 points
176 views
0 votes
1 answer

Convert java code to R code

You can rjava package for this purpose. rJava is ...READ MORE

answered Nov 16, 2018 in Data Analytics by Maverick
• 10,040 points
410 views
0 votes
1 answer

Error saying "'names' attribute [27] must be the same length as the vector [21]"

When you end up with this error ...READ MORE

answered Nov 12, 2018 in Data Analytics by Maverick
• 10,040 points
334 views
0 votes
1 answer

Error saying " cannot open the connection" when trying to install a package in R

Try install.packages(“package_name”, repos="http://cran.us.r-p ...READ MORE

answered Nov 26, 2018 in Data Analytics by Maverick
• 10,040 points
394 views