17 Manipulare de date (continuare)
tabel_lat <- read.table(header=TRUE, text='
id sex control cond1 cond2
1 M 7.9 12.3 10.7
2 F 6.3 10.6 11.1
3 F 9.5 13.1 13.8
4 M 11.5 13.4 12.9
')
tabel_lat
## id sex control cond1 cond2
## 1 1 M 7.9 12.3 10.7
## 2 2 F 6.3 10.6 11.1
## 3 3 F 9.5 13.1 13.8
## 4 4 M 11.5 13.4 12.9
tabel_lung <- read.table(header=TRUE, text='
id sex condition masuratoare
1 M control 7.9
1 M cond1 12.3
1 M cond2 10.7
2 F control 6.3
2 F cond1 10.6
2 F cond2 11.1
3 F control 9.5
3 F cond1 13.1
3 F cond2 13.8
4 M control 11.5
4 M cond1 13.4
4 M cond2 12.9
')
# transform in factor
tabel_lung$id <- factor(tabel_lung$id)
tabel_lung
## id sex condition masuratoare
## 1 1 M control 7.9
## 2 1 M cond1 12.3
## 3 1 M cond2 10.7
## 4 2 F control 6.3
## 5 2 F cond1 10.6
## 6 2 F cond2 11.1
## 7 3 F control 9.5
## 8 3 F cond1 13.1
## 9 3 F cond2 13.8
## 10 4 M control 11.5
## 11 4 M cond1 13.4
## 12 4 M cond2 12.9
17.1 -> din lat in lung
gather
lat_in_lung <- gather(tabel_lat, Masuratoare, Valoare, control:cond2, factor_key = T)
lat_in_lung
## id sex Masuratoare Valoare
## 1 1 M control 7.9
## 2 2 F control 6.3
## 3 3 F control 9.5
## 4 4 M control 11.5
## 5 1 M cond1 12.3
## 6 2 F cond1 10.6
## 7 3 F cond1 13.1
## 8 4 M cond1 13.4
## 9 1 M cond2 10.7
## 10 2 F cond2 11.1
## 11 3 F cond2 13.8
## 12 4 M cond2 12.9
lat_in_lung <- gather(tabel_lat, Masuratoare, Valoare, cond1, cond2, factor_key = T)
lat_in_lung
## id sex control Masuratoare Valoare
## 1 1 M 7.9 cond1 12.3
## 2 2 F 6.3 cond1 10.6
## 3 3 F 9.5 cond1 13.1
## 4 4 M 11.5 cond1 13.4
## 5 1 M 7.9 cond2 10.7
## 6 2 F 6.3 cond2 11.1
## 7 3 F 9.5 cond2 13.8
## 8 4 M 11.5 cond2 12.9
17.2 -> din lung in lat
spread
tabel_lung
## id sex condition masuratoare
## 1 1 M control 7.9
## 2 1 M cond1 12.3
## 3 1 M cond2 10.7
## 4 2 F control 6.3
## 5 2 F cond1 10.6
## 6 2 F cond2 11.1
## 7 3 F control 9.5
## 8 3 F cond1 13.1
## 9 3 F cond2 13.8
## 10 4 M control 11.5
## 11 4 M cond1 13.4
## 12 4 M cond2 12.9
lung_in_lat <- spread(tabel_lung, condition, masuratoare)
lung_in_lat
## id sex cond1 cond2 control
## 1 1 M 12.3 10.7 7.9
## 2 2 F 10.6 11.1 6.3
## 3 3 F 13.1 13.8 9.5
## 4 4 M 13.4 12.9 11.5