Question: standard deviaition using R
1
gravatar for rsabrina93
3.9 years ago by
rsabrina9310
rsabrina9310 wrote:

how to perform standard deviation on multiple rows(27482) which contain multiple columns(702) using R? I tried the code below:

code:

for(i in 1:nrow(zscore)){
    print(i)
    SD=sd(as.numeric(zscore[i,]))
    zscore[i,702]=SD
}
R • 921 views
ADD COMMENTlink modified 3.9 years ago by Devon Ryan97k • written 3.9 years ago by rsabrina9310
3
gravatar for Devon Ryan
3.9 years ago by
Devon Ryan97k
Freiburg, Germany
Devon Ryan97k wrote:

We can use apply and sd functions, see example:

# dummy reproducible data
zscore <- data.frame(matrix(1:10, nrow = 2))
# introduce NA
zscore[1, 2] <- NA
zscore
#   X1 X2 X3 X4 X5
# 1  1 NA  5  7  9
# 2  2  4  6  8 10

# below will give NAs
apply(zscore, 1, sd)
# [1]       NA 3.162278

# we need to remove NAs before getting SD
apply(zscore, 1, sd, na.rm = TRUE)
# [1] 3.415650 3.162278
ADD COMMENTlink modified 3.9 years ago by zx87549.7k • written 3.9 years ago by Devon Ryan97k

Does it calculate for the entire data frame?

ADD REPLYlink written 3.9 years ago by rsabrina9310
3

yes. The apply function will execute the sd() function on each row separatly (as the second paramter of apply() specifies if the rows (=1) or the columns (=2) will be analyzed.

ADD REPLYlink written 3.9 years ago by Nicolas Rosewick9.2k

I get NAs as result for all the rows

ADD REPLYlink written 3.9 years ago by rsabrina9310
2

Perhaps you need to zscore = as.numeric(zscore) first or use the na.rm=T option.

ADD REPLYlink written 3.9 years ago by Devon Ryan97k

I am assuming Devon's example works fine for you and the problem is when you apply it to your data. Maybe there is a problem with your data not being all numeric, e.g. if you have a data frame with other variables (an so Devon's suggestion about using as.numeric() may or may not work). You could post a few lines of your dataset so that we can take a look at it.

ADD REPLYlink written 3.9 years ago by ddiez1.9k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1481 users visited in the last hour