4

I used the following function to find standard error of variables. but when I run the same function now, it is not working.

st.err <- function(x) {
    sd(x)/sqrt(length(x))
     }

I am getting an error while running this command.

Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) : Calling var(x) on a factor x is defunct. Use something like 'all(duplicated(x)[-1L])' to test for a constant vector. Calls: ... std.error -> unlist -> sapply -> lapply -> FUN -> var Execution halted

My Rstudio is up to date, as well as all the packages I use. Could any of you suggest an alternate function to get standard error?

Varadhu

  • the error message seems to mean that `x` is a factor and cannot be converted to numeric. What does `class(x)` give you? – Lennyy Sep 19 '19 at 21:32
  • class(x) gives me error "object(x) not found". i used this function in the following command to get aggregate. ``` aggregate( list(VIG=DM_data_wide$VIG,BP=DM_data_wide$BP,PROC=DM_data_wide$PROC,HYP=DM_data_wide$HYP), by=list(DM_data_wide$gender),FUN=st.err)``` – Varadharajan Jayakumar Sep 19 '19 at 21:39
  • could you insert the variable that you insert in `st.err(x)` in `class(x)` and try again? – Lennyy Sep 19 '19 at 21:40
  • And BTW, Welcome to SO! Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and update your question so others can help you more easily – Lennyy Sep 19 '19 at 21:41
  • please post a [mcve] ... – Ben Bolker Sep 19 '19 at 21:43
  • 1
    Thank you all for your valuable suggestions. This is my first time trying to post something in this group. i will try my best next time to post better. But, as @Lennyy suggested, one of my variable is a factor and removing that variable from this analysis helped in eliminating the error. – Varadharajan Jayakumar Sep 19 '19 at 21:58
  • 2
    Voting to close as a typo-equivalent. (It was a very informative error message.) – IRTFM Sep 19 '19 at 23:40

1 Answers1

0

I had this happen recently when my data source changed. I was running source code on time series data that previously did not have a header row in the .csv file.

The new data source added a header. I removed the header and solved the error for my code. Hope this helps.