• Register
0 votes
316 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

2 votes
2 answers 222 views
222 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 4 views
4 views
Problem: I want to know the number of the last row : data_ranking <- reduced_data[order(reduced_data$outcome,reduced_data$hospital,na.last=NA),] nobs <- nrow(data_ranking) gives me different results of data_ranking <- reduced_data[order(reduced_data$outcome,reduced_data$hospital,na.last=NA), ... why is that. It seems that nrowgives me the answer I'm looking for, but I don't understand why.
asked Mar 10 Shovo210 13.2k points
0 votes
1 answer 3 views
3 views
Problem: i don&rsquo;t understand this problem. please help me? names' attribute [2] must be the same length as the vector [1]
asked 20 hours ago Rohit kr 14.7k points
1 vote
1 answer 1.2K views
1.2K 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
0 votes
1 answer 4 views
4 views
Problem: Please help me with my query. How to remove rows in r based on condition?.
asked Apr 3 Sana8989 7.7k points
1 vote
1 answer 4 views
4 views
Problem: I am very new to R, and I could not find a simple example online of how to remove the last n characters from every element of a vector (array?) I come from a Java background, so what I would like to do is to iterate over every element of a$data and remove the last 3 characters from every element. How would you go about it?
asked Mar 10 Shovo210 13.2k points
0 votes
1 answer 4 views
4 views
Problem: i don&rsquo;t understand about this problem please help? error in aperm.default(map, c(2, 1, 3)) : invalid first argument, must be an array
asked Apr 1 Rohit kr 14.7k points
1 vote
1 answer 6 views
6 views
Problem: I&rsquo;m new in data frame. I want to generate random numbers using runif(n=1,min=0,max=1) in R, but I don't want the point 0 and 1, how can I modify this to achieve my goal? I think it really generates 1 some way, see my code here randomDim = round(runif(1)*3)+1;randomDim # [1] 4
asked Mar 10 Shovo210 13.2k points
1 vote
1 answer 179 views
179 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
0 votes
1 answer 38 views
38 views
Problem: comparison (1) is possible only for atomic and list types.
asked Feb 22 Dan phillip 4.1k points