Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 202012

Convert from long to wide format with multiple unique variables to other unique variables in R

$
0
0

I am trying to convert from long to wide format but multiple columns denote the unique rows. In the example below, the block, density, species columns denote the unique individuals. Every individual has 2 or 3 rows associated with area and size. I want to convert the area and size to wide format.

This is my dataset

block <- c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2)
species <- c("A","A","A","A","B","B","B","B","A","A","A","A","B","B","B","B","B")
den <- c("20","20","50","50","20","20","50","50","20","20","50","50","20","20","50","50","50")


block <- as.factor(block)
den <- as.factor(den)
species <- as.factor(species)

area <- c(1:17)
size <- c(17:33)

df <- data.frame(block, species, den, area, size)

I want to final dataset with only the unique individuals

  block   species   den   area.1  area.2  area.3  size.1  size.2  size.3
    1        A       20     1       2       NA      17      18      NA
    1        A       50     3       4       NA      19      20      NA
    .....
    2        B       50     15      16      17      31      32      33

Note: Other answers that I have persued do not use multiple columns to denote the uniquness of the rows


Viewing all articles
Browse latest Browse all 202012

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>