AnswerBun.com

mean(,na.rm = TRUE) returns NA and Warning Message

Here is my code and the error/warning message

library(dplyr)

df_sw = as.data.frame(starwars)

df_sw = df_sw[1:11]
final = ""
print(mean(df_sw[,1],trim = 0, na.rm = TRUE))
for(i in 1:ncol(df_sw)){
  print("hello")
  cat(colnames(df_sw)[i], ": ",ifelse(is.numeric(df_sw[1,i]),cat("numeric the mean is: ",mean(df_sw[,i],trim = 0, na.rm = TRUE)),"character: "))
  for(b in 1:4){
    cat(df_sw[b,i], " ")
  }
}

[1] "hello"
name : character: Luke Skywalker C-3PO R2-D2 Darth Vader [1] "hello"
numeric the mean is: 174.358. Error in ans[ypos] <- rep(yes, length.out = len)[ypos] :
replacement has length zero
In addition: Warning message:
In rep(yes, length.out = len) : ‘x’ is NULL so the result will be NULL

I am very new to R. For some reason, my mean() function only returns part of the mean before throwing an error. I tried writing the

mean(df_sw[,I], na.rm = TRUE)

outside the for loop, and it returned both NA and the error/warning.

Any help would be greatly appreciated

Stack Overflow Asked by Dylan Ong on January 5, 2022

1 Answers

One Answer

We could change the ifelse to if/else

for(i in seq_along(df_sw)){
   
   # // if the column is numeric
   if(is.numeric(df_sw[,i])) {
   # // print the mean
   cat(colnames(df_sw)[i], ": numeric the mean is: ",
             mean(df_sw[,i],trim = 0, na.rm = TRUE), "n")
   } else {
    # // print that it is a character column
     cat(colnames(df_sw)[i], ": character: n")
   
    }
 }

#name : character: 
#height : numeric the mean is:  174.358 
#mass : numeric the mean is:  97.31186 
#hair_color : character: 
#skin_color : character: 
#eye_color : character: 
#birth_year : numeric the mean is:  87.56512 
#sex : character: 
#gender : character: 
#homeworld : character: 
#species : character: 

Answered by akrun on January 5, 2022

Add your own answers!

Related Questions

Print [Python] seen as a keyword in Pycharm

1  Asked on February 20, 2021 by jleno46

     

Express req.subdomains empty

1  Asked on February 20, 2021

   

How to exclude data in mySQL string?

2  Asked on February 20, 2021 by markus

     

ERROR at line 3: ORA-00911: invalid character” when joining tables

2  Asked on February 19, 2021 by sinafarheidar12

   

How to transport data from csv to Excel

2  Asked on February 19, 2021 by tangercity

         

How to call Ajax datatables with parameter ASP.NET MVC?

3  Asked on February 19, 2021 by truong-hoang

       

Hide several tds inside the table

2  Asked on February 19, 2021 by tree

     

Kubernetes Conditions.Ready and ContainersReady are false

0  Asked on February 19, 2021 by user3877654

 

Highlight multiple cells using mxCellHighlight

2  Asked on February 18, 2021 by boris

   

How to get all dates up to a certain date?

1  Asked on February 18, 2021 by beck

   

How do I write a script that alerts a input value given by a visitor?

3  Asked on February 18, 2021 by mehmet-akif-vardar

 

Why is “==” not acceptable here?

1  Asked on February 18, 2021 by askman

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.