[R] Comparison of vectors in a matrix

esterhazy a.gaudeul at gmail.com
Wed Nov 11 19:02:03 CET 2009


Yes, thanks for this, this is exactly what I want to do.

However, I have a remaining problem which is how to get R to understand that
each entry in my matrix is a vector of names.

I have been trying to import my text file with the names in each vector of
names enclosed in quotes and separated by commas, or separated by spaces, or
without quotes, etc, with no luck. 

Everytime, R seems to consider the vector of names as just one long name.

In my first colum, for example, I have "henry", in the second, "mary",
"ruth", and in the third "mary", "joseph", and I have no idea how to get R
to see that "mary", "ruth", for example, is composed of two strings of text,
rather than just one.

Thanks for any further help!

http://old.nabble.com/file/p26305756/ffoexample.txt ffoexample.txt 

Tony Plate wrote:
> 
> Nice problem!
> 
> If I understand you correctly, here's how to do it (with list-based
> matrices):
> 
>> set.seed(1)
>> (x <- matrix(lapply(rpois(10,2)+1, function(k) sample(letters[1:10],
>> size=k)), ncol=2, dimnames=list(1:5,c("A","B"))))
>   A           B          
> 1 Character,2 Character,5
> 2 Character,2 Character,5
> 3 Character,3 Character,3
> 4 Character,5 Character,3
> 5 Character,2 "i"        
>> x[1,1]
> [[1]]
> [1] "c" "b"
> 
>> x[1,2]
> [[1]]
> [1] "c" "d" "a" "j" "f"
> 
>> (y <- cbind(x, "A-B"=apply(x, 1, function(ab) setdiff(ab[[1]],
>> ab[[2]]))))
>   A           B           A-B        
> 1 Character,2 Character,5 "b"        
> 2 Character,2 Character,5 "g"        
> 3 Character,3 Character,3 Character,3
> 4 Character,5 Character,3 Character,2
> 5 Character,2 "i"         Character,2
>> y[1,3]
> [[1]]
> [1] "b"
> 
>> 
> 
> -- Tony Plate
> 
> esterhazy wrote:
>> Hi,
>> 
>> I have a matrix with two columns, and the elements of the matrix are
>> vectors.
>> 
>> So for example, in line 3 of column 1 I have a vector v31=("marc",
>> "robert,
>> "marie").
>> 
>> What I need to do is to compare all vectors in column 1 and 2, so as to
>> get,
>> for example setdiff(v31,v32) into a new column.
>> 
>> Is there a way to do this in R?
>> 
>> Thanks!
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://old.nabble.com/Comparison-of-vectors-in-a-matrix-tp26284855p26305756.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list