xx
head(xx,1)
Sport variable 2012.07.01 2012.07.02 2012.07.03 2012.07.04 2012.07.05 2012.07.06 2012.07.07 2012.07.08 2012.07.09 2012.07.10 2012.07.11 2012.07.12 2012.07.13 2012.07.14 2012.07.15 2012.07.16 2012.07.17
1 Soccer Likes 13 13 14 12 11 11 NA 9 16 11 12 15 10 NA 13 9 10
2012.07.18 2012.07.19 2012.07.20 2012.07.21 2012.07.22 2012.07.23 2012.07.24 2012.07.25 2012.07.26 2012.07.27 2012.07.28 2012.07.29 2012.07.30 2012.07.31 2012.08.01 2012.08.02 2012.08.03 2012.08.04 2012.08.05
1 16 10 17 NA 10 15 14 11 11 13 NA 13 26 987 898 162 146 NA 257
2012.08.06 2012.08.07 2012.08.08 2012.08.09 2012.08.10 2012.08.11 2012.08.12 2012.08.13 2012.08.14 2012.08.15 2012.08.16 2012.08.17 2012.08.18 2012.08.19 2012.08.20 2012.08.21 2012.08.22 2012.08.23 2012.08.24
1 370 443 490 612 646 NA 311 371 432 512 610 734 NA 1002 931 886 190 317 386
2012.08.25 2012.08.26 2012.08.27 2012.08.28 2012.08.29 2012.08.30 2012.08.31 2012.09.01 2012.09.02 2012.09.03 2012.09.04 2012.09.05 2012.09.06 2012.09.07 2012.09.08 2012.09.09 2012.09.10 2012.09.11 2012.09.12
1 NA 586 812 904 863 941 922 NA 150 146 175 132 254 330 NA 198 281 254 316
2012.09.13 2012.09.14 2012.09.15 2012.09.16 2012.09.17 2012.09.18 2012.09.19 2012.09.20 2012.09.21 2012.09.22 2012.09.23 2012.09.24 2012.09.25 2012.09.26 2012.09.27 2012.09.28 2012.09.29 2012.09.30 2012.10.01
1 416 594 NA 668 745 972 984 885 496 NA 687 734 767 832 965 934 NA 200 225
2012.10.02 2012.10.03 2012.10.04 2012.10.05 2012.10.06 2012.10.07 2012.10.08 2012.10.09 2012.10.10 2012.10.11 SD Mean Max Min mean
1 219 181 198 229 NA 364 431 492 592 612 336.9102 NA soccer 9 NA
trying to calculate row standard deviation, mean, max, min etc per each row with the following formula:
transform(xx, SD=apply(xx,1, sd, na.rm = TRUE))
transform(xx, Mean=apply(xx,1, mean, na.rm = TRUE))
transform(xx, Max=apply(xx,1, max, na.rm = TRUE))
transform(xx, Min=apply(xx,1, min, na.rm = TRUE))
I dont think this is working since my first two columns are text rather than all numbers.
Is there a way to just calculate numbers in row based calculations?
You can use [ to select the relevant variables as in:
set.seed(007)
X <- data.frame(matrix(sample(c(10:20, NA), 100, replace=TRUE), ncol=10))
sex <- sample(c('F', 'M'), 10, T)
reg <- sample(c('N', 'S', 'E', 'W'), 10, T)
DF <- cbind(sex, reg, X)
DF # this is your data.frame
sex reg X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 F E NA 12 17 18 19 16 12 13 20 14
2 F S 14 12 13 13 14 18 16 17 20 10
3 F N 11 19 NA 12 19 19 19 20 12 20
4 F E 10 11 20 12 15 17 18 17 18 12
5 M E 12 15 NA 14 20 18 16 11 14 18
6 F E 19 11 10 20 13 14 17 16 10 16
7 M E 14 16 17 15 10 11 15 15 11 16
8 F W NA 10 15 19 19 12 15 15 19 14
9 M N 11 NA NA 20 20 14 14 17 14 19
10 F W 15 13 14 15 NA 13 15 NA 15 12
As you can see the first to varibles are non-numeric. use sapply(DF, class) to see that.
Now using [ as mentioned above you can choose all the numeric variables
DF[,-c(1,2)] # selecting all variables but 1 and 2
You can now perform your calculations on these varibles
transform(DF, SD=apply(DF[,-c(1,2)],1, sd, na.rm = TRUE)) # and so on
sex reg X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 SD
1 F E NA 12 17 18 19 16 12 13 20 14 3.041381
2 F S 14 12 13 13 14 18 16 17 20 10 3.020302
3 F N 11 19 NA 12 19 19 19 20 12 20 3.865805
4 F E 10 11 20 12 15 17 18 17 18 12 3.496029
5 M E 12 15 NA 14 20 18 16 11 14 18 2.958040
6 F E 19 11 10 20 13 14 17 16 10 16 3.596294
7 M E 14 16 17 15 10 11 15 15 11 16 2.449490
8 F W NA 10 15 19 19 12 15 15 19 14 3.201562
9 M N 11 NA NA 20 20 14 14 17 14 19 3.356763
10 F W 15 13 14 15 NA 13 15 NA 15 12 1.195229
Another alternative would be:
newDF <- DF[,sapply(DF, is.numeric)]
transform(DF, SD=apply(newDF,1, sd, na.rm = TRUE))
sex reg X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 SD
1 F E NA 12 17 18 19 16 12 13 20 14 3.041381
2 F S 14 12 13 13 14 18 16 17 20 10 3.020302
3 F N 11 19 NA 12 19 19 19 20 12 20 3.865805
4 F E 10 11 20 12 15 17 18 17 18 12 3.496029
5 M E 12 15 NA 14 20 18 16 11 14 18 2.958040
6 F E 19 11 10 20 13 14 17 16 10 16 3.596294
7 M E 14 16 17 15 10 11 15 15 11 16 2.449490
8 F W NA 10 15 19 19 12 15 15 19 14 3.201562
9 M N 11 NA NA 20 20 14 14 17 14 19 3.356763
10 F W 15 13 14 15 NA 13 15 NA 15 12 1.195229
I prefer this last one since you don't have to know which varible is numeric, R will select them for you.
This would be a better approach
Define a function for basic stats
Stats <- function(x){
Mean <- mean(x, na.rm=TRUE)
SD <- sd(x, na.rm=TRUE)
Min <- min(x, na.rm=TRUE)
Max <- max(x, na.rm=TRUE)
return(c(Mean=Mean, SD=SD, Min=Min, Max=Max))
}
cbind(DF, t(apply(newDF,1, Stats))) # Where newDF is define as above
sex reg X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 Mean SD Min Max
1 F E NA 12 17 18 19 16 12 13 20 14 15.66667 3.041381 12 20
2 F S 14 12 13 13 14 18 16 17 20 10 14.70000 3.020302 10 20
3 F N 11 19 NA 12 19 19 19 20 12 20 16.77778 3.865805 11 20
4 F E 10 11 20 12 15 17 18 17 18 12 15.00000 3.496029 10 20
5 M E 12 15 NA 14 20 18 16 11 14 18 15.33333 2.958040 11 20
6 F E 19 11 10 20 13 14 17 16 10 16 14.60000 3.596294 10 20
7 M E 14 16 17 15 10 11 15 15 11 16 14.00000 2.449490 10 17
8 F W NA 10 15 19 19 12 15 15 19 14 15.33333 3.201562 10 19
9 M N 11 NA NA 20 20 14 14 17 14 19 16.12500 3.356763 11 20
10 F W 15 13 14 15 NA 13 15 NA 15 12 14.00000 1.195229 12 15
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With