I'm trying to do someting with pivot_longer() to make a wide table long, but I Can't quite figure it out.
Here is the head of the dataframe I am trying to manipulate
head(stack)
unique.pair Area.IN Area.NEAR ALLEVEN.IN ALLEVEN.NEAR TREERICH.IN TREERICH.NEAR HEMIAB.IN HEMIAB.NEAR
1 AGFO 1_AGFO 5 100 100 0.7309552 0.3724176 2 1 1.00 0
2 AGFO 27_AGFO 24 100 100 0.8990520 0.6306221 1 0 1.00 0
3 AGFO 6_AGFO 23 100 100 0.7956735 0.7022392 1 1 1.00 0
4 ALFL LAMR.7_ALFL LAMR.103 100 400 0.4425270 0.6838157 4 6 0.50 0
5 APCO 10_APCO 2 400 400 0.5730378 0.5453876 18 19 0.55 0
6 APCO 4_APCO 9 400 400 0.6349441 0.7078960 22 23 0.55 0
Basically, every row is a unique pair of 2 IDs and their corresponding measurements of certain metrics (.IN and.NEAR); I now need to make it so I have two rows per each unique pair, and I split up their metrics .. for example, I was sort of successful in doing this for "ALLEVEN.IN and ALLEVEN.NEAR". I also need the AREA metrics
master.long <- master.JH %>%
select(unique.pair, ALLEVEN.IN, ALLEVEN.NEAR, HEMIAB.IN, HEMIAB.NEAR, Area.IN, Area.NEAR) %>%
pivot_longer(cols = c(ALLEVEN.IN, ALLEVEN.NEAR), names_to = "HEMI", values_to = "ALLEVEN") %>%
pivot_longer(cols = c(Area.IN, Area.NEAR), names_to = "Area", values_to = "Area_sampled") %>%
separate(HEMI, into = c(NA, "HEMI"))%>%
separate(Area, into = c(NA , "AREA")) %>%
mutate(HEMI.status = case_when(HEMI == "IN" & AREA == "IN" ~ "HEMI",
HEMI == "NEAR" & AREA =="NEAR" ~ "NO.HEMI"))
output is :
# A tibble: 6 x 8
unique.pair HEMIAB.IN HEMIAB.NEAR HEMI ALLEVEN AREA Area_sampled HEMI.status
<chr> <dbl> <dbl> <chr> <dbl> <chr> <dbl> <chr>
1 AGFO 6_AGFO 23 1 0 IN 0.796 IN 100 HEMI
2 AGFO 6_AGFO 23 1 0 IN 0.796 NEAR 100 NA
3 AGFO 6_AGFO 23 1 0 NEAR 0.702 IN 100 NA
4 AGFO 6_AGFO 23 1 0 NEAR 0.702 NEAR 100 NO.HEMI
5 AGFO 27_AGFO 24 1 0 IN 0.899 IN 100 HEMI
6 AGFO 27_AGFO 24 1 0 IN 0.899 NEAR 100 NA
2 questions
1.) I see why there are NA's for HEMI.status, but I'm not sure how to tell the code to just drop those values. I can easily do it later, but was wondering if there is a way within pivot longer
2.) Is there any way to do this for all columns with one code of pivot longer for all of the column; I.e. could I incorporate "TREERICH.IN" and "TREERICH.NEAR" into this as well, with the same HEMI column? I tried, but when I say "names_to" = "HEMI" for TREERICH as well (see below) I get an obvious error
master.long <- master.JH %>%
select(unique.pair, ALLEVEN.IN, ALLEVEN.NEAR, HEMIAB.IN, HEMIAB.NEAR, Area.IN, Area.NEAR) %>%
pivot_longer(cols = c(ALLEVEN.IN, ALLEVEN.NEAR), names_to = "HEMI", values_to = "ALLEVEN") %>%
pivot_longer(cols = c(TREERICH.IN, TREERICH.NEAR), names_to = "HEMI", values_to = "TREERICH")
pivot_longer(cols = c(Area.IN, Area.NEAR), names_to = "Area", values_to = "Area_sampled") %>%
separate(HEMI, into = c(NA, "HEMI"))%>%
separate(Area, into = c(NA , "AREA")) %>%
mutate(HEMI.status = case_when(HEMI == "IN" & AREA == "IN" ~ "HEMI",
HEMI == "NEAR" & AREA =="NEAR" ~ "NO.HEMI"))
Hopefully I explained this well enough. Thanks for any help!