I imagine this is very easy but I can't figure it out. Please help me!
I have data from a .csv file with this structure. It has 157 specimens and 21 X and Y coordinates for each one, so in total 3297 rows with 3 columns (specimen, x, y).
Specimen | X | Y |
---|---|---|
specimen 1 | 1.1 | 1.2 |
specimen 1 | 1.3 | 1.4 |
specimen 1 | 1.5 | 1.6 |
specimen 2 | 0.7 | 0.8 |
specimen 2 | 0.9 | 1.0 |
specimen 2 | 1.1 | 1.2 |
But I need it to look like this:
Specimen | X1 | Y1 | X2 | Y2 | X3 | Y3 | ... | X21 | Y21 |
---|---|---|---|---|---|---|---|---|---|
specimen 1 | 1.1 | 1.3 | 1.3 | 1.4 | 1.5 | 1.6 | ... | ... | ... |
specimen 2 | 0.7 | 0.8 | 0.9 | 1.0 | 1.1 | 1.2 | ... | ... | ... |
I tried in R doing this:
mydata <- read.csv("measurements.csv") # read file
mydata_coord <- data.frame(mydata$Specimen, mydata$X, mydata$Y) # Create dataframe with specimen and coordinates
mydata_nested <- mydata %>% nest_by(mydata.Specimen)
But this creates a nested list that looks like this:
How do I make that nested list become a dataframe with pairs of x,y in the same row for each specimen with names x1,y1,x2,y2,...x21,y21? The result would then be a dataframe of 42 columns and 157 rows.