[R] Comparison of vectors in a matrix

David Winsemius dwinsemius at comcast.net
Wed Nov 11 19:25:14 CET 2009


On Nov 11, 2009, at 1:02 PM, esterhazy wrote:

>
> 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.

Those are all subsumed under the R "whitespace" separators that scan  
and the read.* functions use by default.
>
> 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

I copied and pasted into a textConnection:

 > lines <- readLines(textConnection("username	friends	friendof
+ 26376	paradisacorbasi  zannechaos  filmstarusa  jelliclecat  status   
jennilee_rose  obiwaynekenobi  shaycaron  jillthepill  26376  witchy1   
jumbach  oscarwinner  slai  jenniebennie  tirwen  estall  bront   
zarchasmpgmr  pink_kimono  reidzilla  miz_anneliese  ambelies   
jrianne  henry3730  thufer_hawat  mom2bunky  venaeli  grbenda  ikkin56	 
paradisacorbasi  filmstarusa  jelliclecat jennilee_rose shaycaron  
jillthepill 26376 witchy1 jumbach oscarwinner tirwen estall bront  
zarchasmpgmr pink_kimono reidzilla miz_anneliese ambelies jrianne  
henry3730 ahahaha_ha thufer_hawat mom2bunky venaeli grbenda  
donna_stewart gjanyn
+ aaandy	matttt monotony porcelian abrokenstarr seraphimsigrist  
ballena cbaqir _cassyandra darkspree erishkigal pinkdevildances  
lightning_geek a_life_verbatim frozen_wishes lijago tindernight  
crashing_angel sabrina_g giggly_teapot smarties_2087 theinimitable_l  
african_sunset sirenlunaris eagan_bryhtm robomonkeyninja maid_ov_metal  
aubloomiel countdownish queennola pearl007 princess_macaw wolfie_sara  
gypsy_jack may_cash morningchorus vanityflair lyingpeacefully  
squashedfrogs d_e_r_v_i_s_h the_koira lakshmichithra muffinbits  
amaya_aneko jenikaandzhaodi blood_gypsy aura_oneill icemodeled  
wannabesnorlax2	matttt frida monotony porcelian abrokenstarr  
seraphimsigrist ballena cbaqir _cassyandra shepardshadows darkspree  
erishkigal pinkdevildances lightning_geek a_life_verbatim  
frozen_wishes lijago tindernight crashing_angel sabrina_g  
giggly_teapot laudanum_tea smarties_2087 theinimitable_l  
african_sunset sirenlunaris eagan_bryhtm robomonkeyninja aaskie  
maid_ov_metal aubloomiel countdownish queennola pearl007  
princess_macaw wolfie_sara gypsy_jack may_cash morningchorus  
vanityflair lyingpeacefully squashedfrogs d_e_r_v_i_s_h the_koira  
lakshmichithra muffinbits amaya_aneko jenikaandzhaodi blood_gypsy  
aura_oneill icemodeled sixtycents wannabesnorlax2"))

#realLines will bring in everything up to an EOL.

# and then used scan() on the individual lines:

 > col1 <- scan(textConnection(lines[1]), what="character")
Read 3 items
 > col1
[1] "username" "friends"  "friendof"

 > col2 <- scan(textConnection(lines[2]), what="character")
Read 58 items


 > col3 <- scan(textConnection(lines[3]), what="character")
Read 102 items

I have not figured out what you and Tony are doing with these  
collections of character vectors, but this should help with you doing  
the basic data entry.


>
> 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.
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list