-6

when i write mean it worked

mean(b1temp[1, ])

but for standard deviation, it returns NA

sd(b1temp[1, ])

NA

SO, I modified the function but still returns NA

sd(b1temp[1, ], na.rm=FALSE)

NA

my dataset contains only a row. Is this an issue?

Andre Elrico
  • 10,956
  • 6
  • 50
  • 69
  • 9
    You need at least 2 values to get the SD. – user2974951 Oct 26 '18 at 12:29
  • 3
    *"only a row"* or only a column? And it should be `na.rm = TRUE`. – Rui Barradas Oct 26 '18 at 12:45
  • 1
    Only use `rstudio` tag if the problem is caused by the programm `rstudio`. You have problems with your R-code. – Andre Elrico Oct 26 '18 at 12:46
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 26 '18 at 14:46

1 Answers1

0

The problem here is incorrect subsetting of data.frame as in effect when you execute b1temp[1, ] you get only 1 number where the standard deviation is not defined. Which is the reason of getting NA.

By default data.frame data are organized by column, not by row. So to apply sd to your data you should use subsetting for the columns bitemp[, 1].

Please see the code and simulation below:

b1temp <- data.frame(x = 1:10)

b1temp[1, ]
# [1] 1

mean(b1temp[1, ])
# [1] 1

sd(b1temp[1, ])
# [1] NA

sd(1)
# [1] NA

b1temp[1, ]
# [1]  1  2  3  4  5  6  7  8  9 10

sd(b1temp[, 1])
# [1] 3.02765
Artem
  • 3,304
  • 3
  • 18
  • 41