change data format

Hello, I have a dataframe that need to change the format. Current format has Visit as variable. It means the number of visits to a field. It can be two or three, depending on the field ID. In each visit I counted birds and set the count as a column.

This is how the current format looks like:

Property<-c(rep("A",3), rep("B",2))
Field<-c(rep("y",3), rep("f",2))
Visit<-c("1","2","3","1","2")
bird.count<-c(0,1,1,0,1)
df<-cbind.data.frame(Property, Field, Visit, bird.count)

And here is how I need the data look like (same information but different format):

Property<-c("A","B")
Field<-c("y","f")
Visit1<-c(0,0)
Visit2<-c(1,1)
Visit3<-c(1,NA)
df2<-cbind.data.frame(Property, Field, Visit1, Visit2, Visit3)

I appreciate a recommendation to do this with dplyr or other R tool.

Thanks in advanced

You can do it with tidyr, another tidyvers tool.

library(tidyr)

df <- data.frame(
  stringsAsFactors = FALSE,
          Property = c("A", "A", "A", "B", "B"),
             Field = c("y", "y", "y", "f", "f"),
             Visit = c("1", "2", "3", "1", "2"),
        bird.count = c(0, 1, 1, 0, 1)
)

df %>% 
    pivot_wider(id_cols = c(Property, Field),
                names_from = Visit,
                values_from = bird.count,
                names_prefix = "Visit" )
#> # A tibble: 2 × 5
#>   Property Field Visit1 Visit2 Visit3
#>   <chr>    <chr>  <dbl>  <dbl>  <dbl>
#> 1 A        y          0      1      1
#> 2 B        f          0      1     NA

Created on 2022-07-09 by the reprex package (v2.0.1)

that worked great! thanks so much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.