Question

I have a list of elements say:

l <- c("x","ya1","xb3","yb3","ab","xc3","y","xa1","yd4")

Out of this list I would like to make a list of the matching x,y pairs, i.e.

(("xa1" "ya1") ("xb3" "yb3") ("x" "y"))

In essence, I need to capture the X elements, the Y elements and then pair them up: I know how to do the X,Y extraction part:

xelems <- grep("^x", l, perl=TRUE, value=TRUE)
yelems <- grep("^y", l, perl=TRUE, value=TRUE)

An X element pairs up with a Y element when

1. xElem == yElem # if xElem and yElem are one char long, i.e. 'x' and 'y'    
2. substr(xElem,1,nchar(xElem)) == substr(yElem,1,nchar(yElem))

There is no order, i.e. matching xElem and yElem can be positioned anywhere.

I am however not very sure about the next part. I am more familiar with the SKILL programming language (SKILL is a LISP derivative) and this is how I write it:

procedure( get_xy_pairs(inputList "l")
  let(( yElem (xyPairs nil) xList yList)
    xList=setof(i inputList rexMatchp("^x" i))
    yList=setof(i inputList rexMatchp("^y" i))
    when(xList && yList
      unless(length(xList)==length(yList)
    warn("xList and yList mismatch : %d vs %d\n" length(xList) length(yList))
      )
      foreach(xElem xList
        if(xElem=="x"
          then yElem="y"
          else yElem=strcat("y" substring(xElem 2 strlen(xElem)))
        )
        if(member(yElem yList)
          then xyPairs=cons(list(xElem yElem) xyPairs)
          else warn("x element %s has no matching y element \n" xElem)
        )
      )
    )
    xyPairs
  )
)

When run on l, this would return

get_xy_pairs(l)
*WARNING* x element xc3 has no matching y element 
(("xa1" "ya1") ("xb3" "yb3") ("x" "y"))

As I am still new to R, I would appreciate if you folks can help. Besides, I do understand the R folks tend to avoid for loops and are more into lapply ?

Was it helpful?

Solution

Maybe something like this would work. (Only tested on your sample data.)

## Remove any item not starting with x or y
l2 <- l[grepl("^x|^y", l)]

## Split into a list of items starting with x
##   and items starting with y
L <- split(l2, grepl("^x", l2))

## Give "names" to the "starting with y" group
names(L[[1]]) <- gsub("^y", "x", L[[1]])

## Use match to match the names in the y group with
##   the values from the x group. This results in a
##   nice named vector with the pairs you want
Matches <- L[[1]][match(L[[2]], names(L[[1]]), nomatch=0)]
Matches
#     x   xb3   xa1 
#   "y" "yb3" "ya1" 

As a data.frame:

MatchesDF <- data.frame(x = names(Matches), y = unname(Matches))
MatchesDF
#     x   y
# 1   x   y
# 2 xb3 yb3
# 3 xa1 ya1

OTHER TIPS

I would store tuples in a list, i.e:

xypairs
[[1]]
[1] "x"    "y"

[[2]]
[2] "xb3"  "yb3"

Your procedure can be simplified with match and substring.

xends <- substring(xelems, 2)
yends <- substring(yelems, 2)
ypaired <- match(xends, yends)  # Indices of yelems that match xelems

# Now we need to handle the no-matches:
xsorted <- c(xelems, rep(NA, sum(is.na(ypaired))))
ysorted <- yelems[ypaired]
ysorted <- c(ysorted, yelems[!(yelems %in% ysorted)])

# Now we create the list of tuples:
xypairs <- lapply(1:length(ysorted), function(i) {
  c(xsorted[i], ysorted[i])
})

Result:

xypairs
[[1]]
[1] "x" "y"

[[2]]
[1] "xb3" "yb3"

[[3]]
[1] "xc3" NA   

[[4]]
[1] "xa1" "ya1"

[[5]]
[1] NA    "yd4"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top