Entering edit mode
7.8 years ago
rsabrina93
▴
10
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
}
Does it calculate for the entire data frame?
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.
I get NAs as result for all the rows
Perhaps you need to
zscore = as.numeric(zscore)
first or use thena.rm=T
option.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.