-2
N <- c(1,3,4,6)
a <- c(3,4,5,6)
b <- c(4,5,6,7)
w <- c(5,6,7,6)
dat1 <- data.frame(N,May = a, April = b,June = w)

    N May April June
1    1   3     4    5
2    3   4     5    6
3    4   5     6    7
4    6   6     7    6

I need a data frame, where each value is sd of N value and row value

sd(c(1,3) sd(c(1,4) sd(c(1,5) # for 1st row
sd(c(3,4) sd(c(3,5) sd(c(3,6) # for second and so on.
Roland
  • 127,288
  • 10
  • 191
  • 288
  • 2
    Don't post pictures of data. Provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make it easier to help you. – MrFlick May 10 '16 at 14:24
  • What do you want your result to look like? if I understand correctly you want a matix of 4 x 3 of Standard deviations? – Derek Corcoran May 10 '16 at 14:28
  • 1
    You should never use `c` as an object name in R, it will llead to bugs which can be very annoying to identify, – Molx May 10 '16 at 14:33
  • @Molx R is smart enough to distinguish a function from a vector. The only potential problems are readability of code and sometimes error messages that are hard to understand. – Roland May 10 '16 at 14:35
  • @Roland Indeed but I've had problems in the past because I had `c` in the environment and since the code was correct it took me a long time to fix it. Also if it's a function instead of a vector the damage is done, which is why I prefer the never-ever rule. – Molx May 10 '16 at 14:41
  • @DerekCorcoran, yes, exactly – Igor Glushkov May 10 '16 at 14:42

2 Answers2

0

Try this:

The data:

Norm <- c(1,3,4,6)
a <- c(3,4,5,6)
b <- c(4,5,6,7)
w <- c(5,6,7,6)
mydata <- data.frame(Norm=Norm,May = a, April = b,June = w)

Solution:

finaldata <- do.call('cbind',lapply(names(mydata)[2:4], function(x) apply(mydata[c("Norm",x)],1,sd)))

I hope it helps.

Piece of advice:

Please refrain from using names like data and norm for your variable names. They can easily conflict with things that are native to R. For example norm is a function in R, and so is data.

Abdou
  • 12,931
  • 4
  • 39
  • 42
0

I think I got it

x=matrix(data=NA, nrow=4, ncol=3)
for(j in 1:3){
  for(i in 1:4){
  x[i, j] <-  sd(data[i, c(i,(j+1))])
  x
  }
}
Derek Corcoran
  • 3,930
  • 2
  • 25
  • 54