Friday 27 September 2013

apply-strsplit-rowwise including sort and nested paste

apply-strsplit-rowwise including sort and nested paste

I guess I just don't see it, but all the similar thing I found on the Net,
in the Mailinglist archives or the FAQ could not really elucidate my
issue.
The closest I have found was this: apply strsplit rowwise
I have a df, with two character columns and one numerical column. Filled
like this:
df=data.frame(name1=c("A","B","C","D"),
name2=c("B","A","D","C"),
nums=c(1,1,4,4),
stringsAsFactors=F)
Now I would like to find the unique rows in this, however, only based on
the two name columns. And for those columns, the order of the columns has
no significance, thus i can not use duplicated, if I understood it
correctly.
So I thought about combining the two name columns row wise, make a rowwise
sorting, and print out a paste of the vector (length=2 in combination with
something like sapply).
However I did not get it to work.
So far, I used a for loop, but this takes ages on the original data.
for(i in 1:length(df$name1)){
mysort=sort(c(df$name1[i],df$name2[i]))
df$combname[i]=paste(mysort[1],mysort[2])
}
Any suggestions are welcome. Maybe I just understand unique and sapply in
a wrong way.

No comments:

Post a Comment