• Register
0 votes
203 views

Problem :

I am very new to r programming. I have the array and I want to implement the below method:

x<-function(b){
  if (b>0){
    b/sum(b)
  }
  else 1
}

This method is written to check if there is any value which is larger than the 0 and if it says yes then it needs to divide each element by a sum of a total.

Or else it should just be 1.

But I face below warning message:

“Warning message: r the condition has length > 1 and only the first element will be used”.
How can I fix the above error message?
8 4 2
2,300 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I had also faced such kind of errors when I began programming. I can see your if statement is currently not vectorized. To make it vectorized if statements you must use the ifelse. In your method you can write it as shown below:
x <- function(b){
if (any(b>0)){
  b/sum(b)
}
  else 1
}

or you can try the short vectorised version as shown below

ifelse(b > 0, b/sum(b), 1)

It is upto you which way you want to use, as your first function gives you the output vector of length 1 (in the else part) and the ifelse gives you the output vector of length which is equal to length of b.

5 2 1
4,980 points

Related questions

1 vote
1 answer 729 views
729 views
Problem: I have limited knowledge of R and ggplot. I have recently written a function and trying to run the function as shown below: ggplot(datfr, aes(x = dat1[1:951,], y = dat2[1:951,])) + geom_point() After executing above function I am facing below error: Don't ... and when I try to use nrow on my two datasets it again returns the same number of rows. How can I fix above ggplot related error.
asked May 26, 2020 Martin K 6.6k points
2 votes
2 answers 187 views
187 views
Problem: Hello all, I am a noob to this java programming. I started learning it a few days back. Re-type the code and fix any errors. the code should convert non-positive numbers to 1. As long as I understand this problem is sying to convert a negative number to ... but that&rsquo;s not enough for my case. If your understanding is up to this level, please pull me up from the grave LOL. Thanks.
asked Mar 19, 2020 Gavin 15.3k points
1 vote
1 answer 73 views
73 views
Problem: I am very new to R programming. I have recently started learning R programming with the help of the online tutorials and I am also referring few books available on R programming. I have my one dataframe named dih_y2. Below mentioned two lines are giving me ... length I am unable to understand above error due to my limited knowledge on R. Kindly guide me in fixing above R related error.
asked May 27, 2020 Martin K 6.6k points
1 vote
2 answers 219 views
219 views
Problem : I am very new to R. Currently I want to split the column of my data frame (menuA) is as shown below Column1 1|3|4|5 4|5|6|7 I was trying to split my numbers in above columnA so I coded it as shown below. menuA <- data.frame (do.call('rbind', ... 120"),  :   number of columns of result is not a multiple of vector length (arg 1) Please let me know if all my data is separated properly?
asked Apr 22, 2020 stewart 4k points
0 votes
2 answers 1.3K views
1.3K views
Problem : I have the RScript file (let's call it main.r) which has the reference to another file, using the following code: source("functions.R") But, when i run a RScript file, it complains with the following error: Error in file(filename, "r", encoding = encoding) ... next to the functions.R in a same directory. I can call a functions.R in a RMd (RMarkdown) file which exist in a same directory
asked Jan 17, 2020 jwilliam 3.9k points
0 votes
2 answers 799 views
799 views
Problem : I have below error message on the django 1.4: &ldquo;Dictionary update sequence element #0 has length 1; 2 is required&rdquo; It happened to me when I tried using a template tag like: `{% for v in values %} It happens too when I try to access ... rendering too. I just restarted the uwsgi and everything works well, but above error comes back later. Has someone an idea to fix this error?
asked Jan 22, 2020 jwilliam 3.9k points
0 votes
1 answer 211 views
0 votes
1 answer 880 views
880 views
Problem : I face folowing error when I run an ANOVA on R in model.response(mf, "numeric") : using type = "numeric" with a factor response will be ignored
asked Nov 12, 2019 peterlaw 6.9k points
0 votes
1 answer 7 views
7 views
Problem: Hi! I am a newbie in the world of programming. I am learning decision constructs in C++. I came across following code on a website: #include<iostream> #include<string> using namespace std; int main() { float f = 0.1; if (f=0.1) ... used, but in this program, the programmer has used assignment operator in &lsquo;if&rsquo; statement. Can anybody please explain the program and output please?
asked Nov 29, 2020 Code Learner 5.1k points