user5946647
user5946647

Reputation:

R: Repeat script n times, changing variables in each iteration

I have a script that I want to repeat n times, where some variables are changed by 1 each iteration. I'm creating a data frame consisting of the standard deviation of the difference of various vectors. My script currently looks like this:

standard.deviation <- data.frame
c(
 sd(diff(t1[,1])),
 sd(diff(t1[,2])),
 sd(diff(t1[,3])),
 sd(diff(t1[,4])),
 sd(diff(t1[,5]))
 ),
c(
  sd(diff(t2[,1])),
  sd(diff(t2[,2])),
  sd(diff(t2[,3])),
  sd(diff(t2[,4])),
  sd(diff(t2[,5]))
 ), 
c(
  sd(diff(t3[,1])),
  sd(diff(t3[,2])),
  sd(diff(t3[,3])),
  sd(diff(t3[,4])),
  sd(diff(t3[,5]))
 ),
)

I want to write the script creating the vector only once, and repeat it n times (n=3 in this example) so that I end up with n vectors. In each iteration, I want to add 1 to a variable (in this case: 1 -> 2 -> 3, so the number next to 't'). t1, t2 and t3 are all separate data frames, and I can't figure out how to loop a script with changing data frame names.

1) How to make this happen?

2) I would also like to divide each sd value in a row by the row number. How would I do this?

3) I will be using 140 data frames in total. Is there a way to call all of these with a simple function, rather than making a list and adding each of the 140 data frames individually?

Upvotes: 2

Views: 2534

Answers (3)

slamballais
slamballais

Reputation: 3235

Assuming that you always want to use columns 1 to 5...

# some data 
t3 <- t2 <- t1 <- as.data.frame(matrix(rnorm(100),10,10))

# script itself
lis=list(t1,t2,t3)
sapply(lis,function(x) sapply(x[,1:5],function(y) sd(diff(y))))

#        [,1]     [,2]     [,3]
# V1 1.733599 1.733599 1.733599
# V2 1.577737 1.577737 1.577737
# V3 1.574130 1.574130 1.574130
# V4 1.158639 1.158639 1.158639
# V5 0.999489 0.999489 0.999489

The output is a matrix, so as.data.frame should fix that.

For completeness: As @Tensibai mentions, you can just use list(mget(ls(pattern="^t[0-9]+$"))), assuming that all your variables are t followed by a number.

Edit: Thanks to @Tensibai for pointing out a missing step and improving the code, and the mget step.

Upvotes: 3

Tensibai
Tensibai

Reputation: 15784

Use functions to get a more readable code:

set.seed(123) # so you'll get the same number as this example
t1 <- t2 <- t3 <- data.frame(replicate(5,runif(10)))

# make a function for your sd of diff
sd.cols <- function(data) {
  # loop over the df columns
  sapply(data,function(x) sd(diff(x)))
}

# make a list of your data frames
dflist <- list(sdt1=t1,sdt2=t2,sdt3=t3)
# Loop overthe list
result <- data.frame(lapply(dflist,sd.cols))

Which gives:

> result
       sdt1      sdt2      sdt3
1 0.4887692 0.4887692 0.4887692
2 0.5140287 0.5140287 0.5140287
3 0.2137486 0.2137486 0.2137486
4 0.3856857 0.3856857 0.3856857
5 0.2548264 0.2548264 0.2548264

Upvotes: 4

kpie
kpie

Reputation: 11100

You can itterate through a list of the ts...

ans <- data.frame()
dats <- c(t, t1 , t2)
for (k in  dats){
     temp <- c()
     for (k2 in c(1,2,3,4,5)){
          temp <- c(temp , sd(k[,k2]))
     }
ans <- rbind(ans,temp)
}

rownames(ans) <- c("t1","t2","t3")
colnames(ans) <- c(1,2,3,4,5)
attr(results,"title") <- "standard deviation"

Upvotes: 1

Related Questions