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

+1 vote

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
• 11,360 points
3,648 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,840 points

Related Questions In Data Analytics

+1 vote
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,840 points
7,997 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,840 points
2,043 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,840 points
1,765 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,840 points
3,769 views
+1 vote
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,840 points
4,096 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://cr ...READ MORE

answered Nov 26, 2018 in Data Analytics by Maverick
• 10,840 points
9,153 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP