Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

r - Table of Interactions - Case with pets and houses

I have a list of houses and a list of animal species.

houses = c(1,1,2,3,4,4,4,4,5,6,5)
animals = c('cat','dog','cat','dog','rat', 'cat', 'spider', 'snake', 'cat', 'cat', 'rat')

I am trying to create a function that returns an upper triangular table that indicates for each pet, the number of times that it was observed to live in the same house than the other animal species. Does it make sense?

For the above example, the table should look like this (hope there's no mistake!) :

    dog   rat   spider   snake
cat  1     2      1        1      
dog        0      0        0
rat               1        1
spider                     1

Note: This function should work for any two vectors of same lengths, whatever if they contain numbers or string

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use table and crossprod:

out <- crossprod(table(houses, animals))
out[lower.tri(out, diag=TRUE)] <- NA
out
#         animals
# animals  cat dog rat snake spider
#   cat     NA   1   2     1      1
#   dog     NA  NA   0     0      0
#   rat     NA  NA  NA     1      1
#   snake   NA  NA  NA    NA      1
#   spider  NA  NA  NA    NA     NA

Since the output is a matrix you can suppress the printing of the NA values directly in print:

print(out,na.print="")
#         animals
# animals  cat dog rat snake spider
#   cat          1   2     1      1
#   dog              0     0      0
#   rat                    1      1
#   snake                         1
#   spider                         

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...