Reputation: 77
How can I split one variable values as columns of the data frame according to the levels of the other grouping variable?
Suppose I have a data frame as shown below
Site Species dbh
1 sp1 2.8
1 sp2 2.2
2 sp1 4.0
2 sp2 1.5
3 sp1 3.9
3 sp2 2.5
I want to get the output as below in which the levels of the grouping variable (species) become columns of the data frame and dbh values as values for each level of the grouping variable.
Site sp1 sp2
1 2.8 2.2
2 4.0 1.5
3 3.9 2.5
I would be thankful for your valuable suggestions.
Regards,
Farhan
Upvotes: 0
Views: 113
Reputation: 102700
Try reshape
if you are with base R
reshape(
df,
direction = "wide",
idvar = "Site",
timevar = "Species"
)
which gives
Site dbh.sp1 dbh.sp2
1 1 2.8 2.2
3 2 4.0 1.5
5 3 3.9 2.5
Upvotes: 0
Reputation: 17823
This is called a “reshape” or a “pivot”. There are hundreds of tutorials and SO questions out there about it.
dat <- read.table(header = TRUE, text = "
Site Species dbh
1 sp1 2.8
1 sp2 2.2
2 sp1 4.0
2 sp2 1.5
3 sp1 3.9
3 sp2 2.5")
With the tidyverse
:
library(tidyr)
dat %>% pivot_wider(values_from = "dbh", names_from = "Species")
#> # A tibble: 3 x 3
#> Site sp1 sp2
#> <int> <dbl> <dbl>
#> 1 1 2.8 2.2
#> 2 2 4 1.5
#> 3 3 3.9 2.5
With data.table
:
library(data.table)
setDT(dat)
dcast(dat, Site ~ Species)
#> Using 'dbh' as value column. Use 'value.var' to override
#> Site sp1 sp2
#> 1: 1 2.8 2.2
#> 2: 2 4.0 1.5
#> 3: 3 3.9 2.5
Upvotes: 1