-1

I am relatively new to R and want to change a dataframe I am working with from wide to tall.

The data I have right now looks something like this:

df.wide<-read.csv(header=T, text="
ID, Group, left.hemisphere.thk, right.hemisphere.thk, left.hemisphere.vol, right.hemisphere.vol, left.hemisphere.sa, right.hemisphere.sa
100, 0, 72, 55, 18, 6, 333, 22
101, 0, 73, 50, 19, 7, 332, 11
103, 0, 72, 49, 20, 1, 300, 14
200, 1, 50, 45, 8, 1, 100, 44
201, 1, 52, 52, 9, 2, 222, 18
203, 1, 50, 33, 10, 10, 100, 15")

I want to convert the data set to be long and look like the following with also creating new variable columns:

# make Hemisphere = 0 (if left) =1 (if right)
ID   Group   Hemisphere     Brain.Data
100    0         0          72
100    0         1          55
100    0         0          18
100    0         1           6
100    0         0         333
100    0         1          22
101    0         0          73
101    0         1          50
101    0         0          19
101    0         1           7
101    0         0         332
.
.
.
.
203    1         1          15
crich
  • 99
  • 3
  • 8

2 Answers2

2

We can use gather from tidyr and create the 'Hemisphere' by recoding the column names to 0 and 1

library(tidyverse)
df.wide %>% 
  gather(Hemisphere, Brain.data, 3:4) %>% 
  mutate(Hemisphere =  group_indices(., Hemisphere) - 1) %>%
  arrange(ID)
#     ID Group Hemisphere Brain.data
#1  100     0          0         72
#2  100     0          1         55
#3  101     0          0         73
#4  101     0          1         50
#5  103     0          0         72
#6  103     0          1         49
#7  200     1          0         50
#8  200     1          1         45
#9  201     1          0         52
#10 201     1          1         52
#11 203     1          0         50
#12 203     1          1         33
akrun
  • 874,273
  • 37
  • 540
  • 662
  • The code returns an error and warning `Error: Column Hemisphere must be length 16 (the group size) or one, not 168 In addition: Warning message: group_indices_.grouped_df ignores extra arguments ` – crich Jul 02 '19 at 17:48
  • @crich Looks like your original data have some grouping info. Try `df.wide %>% ungroup %>% ..` – akrun Jul 02 '19 at 17:51
  • Lastly, what does the `3:4` indicate in the gather line....is that the columns? – crich Jul 02 '19 at 18:05
  • @crich It is the column index. You can also specify the column names. I didn't do it because the column name is too long – akrun Jul 02 '19 at 18:59
  • If I wanted to add more variables like `left.hemisphere.volume` and `right.hemipshere.volume` to also be reshaped, would the I just alter the column index to be `3:6` ? – crich Jul 02 '19 at 19:09
  • @crich Can you please update your example and expected output – akrun Jul 02 '19 at 20:07
1

You could use stats::reshape, with direction = "long":

> df.long <- reshape(df.wide, idvar = "ID", 
+                    times = c("left.hemisphere.thk", "right.hemisphere.thk"), 
+                    timevar = "Hemisphere", 
+                    varying = list(c("left.hemisphere.thk", "right.hemisphere.thk")),
+                    direction = "long")
> rownames(df.long) <- NULL
> colnames(df.long) <- c("ID", "Group", "Hemisphere", "Brain.Data")
> df.long
    ID Group           Hemisphere Brain.Data
1  100     0  left.hemisphere.thk         72
2  101     0  left.hemisphere.thk         73
3  103     0  left.hemisphere.thk         72
4  200     1  left.hemisphere.thk         50
5  201     1  left.hemisphere.thk         52
6  203     1  left.hemisphere.thk         50
7  100     0 right.hemisphere.thk         55
8  101     0 right.hemisphere.thk         50
9  103     0 right.hemisphere.thk         49
10 200     1 right.hemisphere.thk         45
11 201     1 right.hemisphere.thk         52
12 203     1 right.hemisphere.thk         33

Btw try searching for pivoting/unpivoting or casting/melting tables.

Milica
  • 23
  • 7