Combine text across multiple lines in R

My data.frame(Networks)contains the following:

Location <- c("Farm", "Supermarket", "Farm", "Conference", 
         "Supermarket", "Supermarket")
Instructor <- c("Bob", "Bob", "Louise", "Sally", "Lee", "Jeff")
Operator <- c("Lee", "Lee", "Julie", "Louise", "Bob", "Louise")

Networks <- data.frame(Location, Instructor, Operator, stringsAsFactors=FALSE)

MY QUESTION

I want to add a new column Transactions$Countto a new data.frame file Transactionsthat sums the exchanges between each Instructorand Operatorfor each Location EXPECTED EXIT

Location <- c("Farm", "Supermarket", "Farm", "Conference", "Supermarket")
Person1 <- c("Bob", "Louise", "Sally", "Jeff")
Person2 < - c("Lee", "Julie", "Louise", "Louise")
Count < - c(1, 2, 1, 1, 1)
Transactions <- data.frame(Location, Person1, Person2, Count, 
            stringsAsFactors=FALSE) 

For example, there would be a total of 2 exchanges between Bob and Lee at the supermarket. It does not matter if one person is an instructor or operator, I am interested in their exchange. The upcoming issue marks two exchanges between Bob and Lee at the supermarket. There is one exchange for every other combination in other places.


WHAT I SAD
I thought it greplmight come in handy, but I want to iterate over 1300 rows of this data, so it can be expensive. Thank.

+4
2

"data.table" pmin pmax "by".

:

Networks <- data.frame(Location, Instructor, Operator, stringsAsFactors = FALSE)
library(data.table)

as.data.table(Networks)[
  , TransCount := .N, 
  by = list(Location, 
            pmin(Instructor, Operator), 
            pmax(Instructor, Operator))][]
#       Location Instructor Operator TransCount
# 1:        Farm        Bob      Lee          1
# 2: Supermarket        Bob      Lee          2
# 3:        Farm     Louise    Julie          1
# 4:  Conference      Sally   Louise          1
# 5: Supermarket        Lee      Bob          2
# 6: Supermarket       Jeff   Louise          1

, , :

as.data.table(Networks)[
  , c("Person1", "Person2") := list(
    pmin(Instructor, Operator), 
    pmax(Instructor, Operator)), 
  by = 1:nrow(Networks)
][
  , list(TransCount = .N), 
  by = .(Location, Person1, Person2)
]
#       Location Person1 Person2 TransCount
# 1:        Farm     Bob     Lee          1
# 2: Supermarket     Bob     Lee          2
# 3:        Farm   Julie  Louise          1
# 4:  Conference  Louise   Sally          1
# 5: Supermarket    Jeff  Louise          1
+4

library(dplyr)
Networks  %>% 
     group_by(Location, Person1=pmin(Instructor,Operator), 
                       Person2= pmax(Instructor,Operator)) %>% 
     summarise(Count=n()) 
#     Location Person1 Person2 Count
#1  Conference  Louise   Sally     1
#2        Farm     Bob     Lee     1
#3        Farm   Julie  Louise     1
#4 Supermarket     Bob     Lee     2
#5 Supermarket    Jeff  Louise     1

base R

 d1 <-cbind(Location=Networks[,1],
      data.frame(setNames(Map(do.call, c('pmin', 'pmax'), 
            list(Networks[-1])), c('Person1', 'Person2'))))

aggregate(cbind(Count=1:nrow(d1))~., d1, FUN=length)
#     Location Person1 Person2 Count
#1        Farm     Bob     Lee     1
#2 Supermarket     Bob     Lee     2
#3 Supermarket    Jeff  Louise     1
#4        Farm   Julie  Louise     1
#5  Conference  Louise   Sally     1

Networks <- data.frame(Location, Instructor, Operator, 
                  stringsAsFactors=FALSE)
+1

All Articles