Hi all,
I have the following head of data
OID IN_FID NEAR_FID NEAR_DIST
1 1 1 49 63.10777
2 2 2 63 154.24751
3 3 3 4 383.29095
4 4 4 3 383.29095
5 5 5 4 444.65797
6 6 6 171 365.11103
I would like to combine the IN_FID with NEAR_FID, and I did so by doing
paste(df$IN_FID, df$NEAR_FID, sep ="")
but, does anyone know if there is a way to arrange it such that the smaller of the two numbers (IN_FID vs NEAR_FID, always comes first?
Thanks!
This should do the trick. Customize as needed.
library(tidyverse)
library(glue)
dd <-
tribble(
~IN_FID, ~NEAR_FID,
1, 49,
2, 63,
3, 4,
4, 3,
5, 4,
6, 17
)
map2_chr(dd$IN_FID, dd$NEAR_FID,
~glue_collapse(sort(c(.x, .y)), sep = " vs. "))
#> [1] "1 vs. 49" "2 vs. 63" "3 vs. 4" "3 vs. 4" "4 vs. 5" "6 vs. 17"
arnabp
3
Another way of achieving the same thing ... Using the mapply function to find min and max and then combining as desired. Code snippet below might help
df1 <- data.frame(in_fid = c(1, 2, 3, 4, 5, 6), near_fid = c(49, 63, 4, 3, 4, 171))
paste(mapply(min, df1$in_fid, df1$near_fid),
mapply(max, df1$in_fid, df1$near_fid), sep = " ")
Thanks
Arnab P
1 Like
Oh this is great! Thank you! Is there any way to transmute() this, i.e. to take out the IN_FID and NEAR_FID columns after I make the new column?
Thanks
system
Closed
5
This topic was automatically closed 7 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.