Reputation: 461
all.
I am very new to writing loops or functions in R, and I still haven't really understood how to do that. Currently, I need to write a loop/function (not sure which one would be better) to perform several t-tests with different data frames.
I have data that is similar to this:
set.seed(694)
df_1_08 <- data.frame(
year = 2008,
a = runif(100, 0, 100),
b = runif(100, 0, 100),
c = runif(100, 0, 100),
d = runif(100, 0, 100)
)
df_1_09 <- data.frame(
year = 2009,
a = rnorm(100, 0, 1),
b = rnorm(100, 0, 1),
c = rnorm(100, 0, 1),
d = rnorm(100, 0, 1)
)
df_1_10 <- data.frame(
year = 2010,
a = rnorm(100, 0, 1),
b = rnorm(100, 0, 1),
c = rnorm(100, 0, 1),
d = rnorm(100, 0, 1)
)
df_2_08 <- data.frame(
year = 2008,
a = rnorm(100, 0, 1),
b = rnorm(100, 0, 1),
c = rnorm(100, 0, 1),
d = rnorm(100, 0, 1)
)
df_2_09 <- data.frame(
year = 2009,
a = rnorm(100, 0, 1),
b = rnorm(100, 0, 1),
c = rnorm(100, 0, 1),
d = rnorm(100, 0, 1)
)
df_2_10 <- data.frame(
year = 2010,
a = rnorm(100, 0, 1),
b = rnorm(100, 0, 1),
c = rnorm(100, 0, 1),
d = rnorm(100, 0, 1)
)
# Write Loop to do t-test between dfs 08, 09, 10 comparing columns a, b, c, d and storing the full results in a df
Essentially, what I need to do with this data is to run t-tests for specific columns each year (2008, 2009, 2010) so that df_1_08
runs a t-test with df_2_08
in all columns (a
, b
, c
, d
) and then store these in a data frame (with the t-statistic, p-value, etc stored in it). This sounds like a job perfect for a loop. But I also need to do this for each of the years (2008, 2009, and 2010) and store the results in separate data frames, so this sounds like a job perfect for a function.
I'm unsure about how to write either, so I figured I'd ask for some help in writing these loops/functions. Thanks in advance for any help you might provide.
I could also have the dataframes combined into one large df with a column identifying the original data frame number (i.e. df1 or df2) and one column identifying the data frame year (i.e. 2008, 2009, 2010). It would look like this:
df1 <- rbind(df_1_08, df_1_09, df_1_10)
df1$ID <-1
df2 <- rbind(df_1_08, df_1_09, df_1_10)
df2$ID <- 2
master.df <- rbind(df1, df2)
I'm not sure if it would be easier to write a loop/function to run the t.tests with the master.df
. In that df, I would essentially need to do the following within a loop or function:
master.df
into df1
and df2
df1
and df2
in yearst.test
for columns a
, b
, c
, and d
for each yeart.test
outputs (i.e. t-statistic, p-value, etc) in a data.frame that I can then print.Upvotes: 1
Views: 648
Reputation: 3184
How about:
df_1_08 <- data.frame(year = 2008, a = runif(100, 0, 100), b = runif(100, 0, 100), c = runif(100, 0, 100), d = runif(100, 0, 100))
df_1_09 <- data.frame(year = 2009, a = runif(100, 0, 100), b = runif(100, 0, 100), c = runif(100, 0, 100), d = runif(100, 0, 100))
df_1_10 <- data.frame(year = 2010, a = runif(100, 0, 100), b = runif(100, 0, 100), c = runif(100, 0, 100), d = runif(100, 0, 100))
df_2_08 <- data.frame(year = 2008, a = runif(100, 0, 100), b = runif(100, 0, 100), c = runif(100, 0, 100), d = runif(100, 0, 100))
df_2_09 <- data.frame(year = 2009, a = runif(100, 0, 100), b = runif(100, 0, 100), c = runif(100, 0, 100), d = runif(100, 0, 100))
df_2_10 <- data.frame(year = 2010, a = runif(100, 0, 100), b = runif(100, 0, 100), c = runif(100, 0, 100), d = runif(100, 0, 100))
dfs_1.names <- ls()[grep("df_1", ls())]
dfs_2.names <- ls()[grep("df_2", ls())]
dfs_1.list <-lapply(dfs_1.names, get)
dfs_2.list <- lapply(dfs_2.names, get)
#in case you want to try the matrix
dfs_1.mtrx <- do.call("rbind",dfs_1.list)
dfs_2.mtrx <- do.call("rbind",dfs_2.list)
years <- intersect(unique(dfs_1.mtrx[,"year"]),unique(dfs_2.mtrx[,"year"]))
# [1] 2008 2009 2010
columns <- intersect(colnames(dfs_1.mtrx[,-1]),colnames(dfs_2.mtrx[,-1]))
# [1] "a" "b" "c" "d"
df.ttest <- as.data.frame(matrix(NA, ncol = 8, nrow = length(years)*length(columns)))
colnames(df.ttest) <- c("year","column","tstat","p.value","degreesf","low.conf","up.conf","data.name")
count = 0
for(i in 1:length(years)){
for(j in columns){
ttest <- t.test(dfs_1.list[[i]][j], dfs_2.list[[i]][j])
ttest$data.name <- paste(paste0("df_1_",years[i]-2000,"$",j),"and",
paste0("df_2_",years[i]-2000,"$",j))
count <- count + 1
df.ttest[count, "year"] <- years[i]
df.ttest[count, "column"] <- j
df.ttest[count, "tstat"] <- ttest$statistic
df.ttest[count, "p.value"] <- ttest$p.value
df.ttest[count, "degreesf"] <- ttest$parameter
df.ttest[count, "low.conf"] <- ttest$conf.int[1]
df.ttest[count, "up.conf"] <- ttest$conf.int[2]
df.ttest[count, "data.name"] <- ttest$data.name
}
}
df.ttest
Which looks like:
year column tstat p.value degreesf low.conf up.conf data.name
1 2008 a 1.0607688 0.29008725 197.9914 -3.7038792 12.327117 df_1_8$a and df_2_8$a
2 2008 b 0.3311722 0.74086573 197.3689 -6.6956039 9.398291 df_1_8$b and df_2_8$b
3 2008 c 1.0410813 0.29910773 197.9405 -3.7582835 12.164152 df_1_8$c and df_2_8$c
4 2008 d 1.2623350 0.20834791 193.4532 -2.9384999 13.387911 df_1_8$d and df_2_8$d
5 2009 a -0.5764091 0.56500626 194.1686 -10.1442158 5.555762 df_1_9$a and df_2_9$a
6 2009 b -1.5222524 0.12954190 197.9248 -14.4317793 1.857603 df_1_9$b and df_2_9$b
7 2009 c -0.1744245 0.86171283 195.0217 -8.6590932 7.251902 df_1_9$c and df_2_9$c
8 2009 d 0.0839337 0.93319409 197.6654 -7.5768817 8.250526 df_1_9$d and df_2_9$d
9 2010 a 1.9125742 0.05724768 197.7406 -0.2353887 15.378495 df_1_10$a and df_2_10$a
10 2010 b 0.9024489 0.36792603 196.0224 -4.0977460 11.011904 df_1_10$b and df_2_10$b
11 2010 c -0.9735756 0.33145768 197.5899 -12.2641333 4.157135 df_1_10$c and df_2_10$c
12 2010 d 0.8721498 0.38418378 197.8601 -4.5311820 11.717207 df_1_10$d and df_2_10$d
Upvotes: 2