Reshaping data and loop in R

Just a self-reminder.

I have a data frame with multiple columns and rows:

       id       ZT0      ZT4       ZT8      ZT12      ZT16      ZT20     ZT24
Smp_125780 1.070978 1.116298 0.9546574 0.9521279 1.0314895 1.0371307 1.035997
Smp_172730 1.032250 1.112181 1.0911900 0.9228381 0.9148687 0.9533989 1.053520

The first thing I would like to do is to devide each value by the row means. I came up to a for loop:

## calculate relative expression values (value/group mean)
for (i in 1:nrow(z)) {
	for (j in 1:ncol(z)) {
		z[i,j] = z[i,j]/rowMeans(z[i,])
	}
} 

But I didn’t notice that the rowMeans changed after each j loop. So I should difine it before:

for (i in 1:nrow(z)) {
	idmean<-rowMeans(z[i,]); ## define first
	for (j in 1:ncol(z)) {
		z[i,j] = z[i,j]/idmean  ## use afterwards
	}
} 

and I wanted to plot line (different ZT against values) series grouped by id. To this end I need to convert my data into something like:

           id Time       Val
1  Smp_125780  ZT0 1.0709784
2  Smp_172730  ZT0 1.0322495
3  Smp_125780  ZT4 1.1162981
4  Smp_172730  ZT4 1.1121809
5  Smp_125780  ZT8 0.9546574
6  Smp_172730  ZT8 1.0911900
7  Smp_125780 ZT12 0.9521279
8  Smp_172730 ZT12 0.9228381
9  Smp_125780 ZT16 1.0314895
10 Smp_172730 ZT16 0.9148687
11 Smp_125780 ZT20 1.0371307
12 Smp_172730 ZT20 0.9533989
13 Smp_125780 ZT24 1.0359967
14 Smp_172730 ZT24 1.0535199

After searching for some time, I came to the tidyr package with nice documentation: https://uc-r.github.io/tidyr, in which the gather looks nice and easy to use:

## z$id<-rownames(z) # change rownames into a column if necessary
library(tidyr)
long.z <- z %>% gather(Time, Exp, ZT0:ZT44)

DONE!

Z. Lu avatar
Z. Lu
Computer biologist, amature photographer, vintage fan and web lover.
comments powered by Disqus