Skip to content

Constructing the transition pair using loop function and paste()

4 messages · Sally Zhen, jim holtman

#
try this:  You were redfining 'transition' within the loop

x <-
c('A','D','F','H','N','A','C','H','F','D','F','F','H','K','G','D','D','B','N','K','O','V','S','S','F','H','J','U','K','T','H','S','R','G','S','R','R','G','D','B','G','F','G','N','H','H','K','L','B','X','C','V','S','F','X','G','T','H','H','H','R','A','E','D','C')
y <- c(rep(0293,8), rep(0498,6), rep(6847,10), rep(6209,4), rep(8562,13),
rep(4596,6), rep(2857,2), rep(6178,3), rep(6018,5), rep(5629,4),
rep(7535,4))
mydata <- as.data.frame(cbind(x,y))
names(mydata) <- c('actions', 'agents')
mydata

transition <- vector('list', length(unique(mydata$agents)))        #
create a list to hold the outputs

for (i in 1:length(unique(mydata$agents))){         # decompose the
data frame by agents
    agent.i <- mydata[mydata$agents == (unique(mydata$agents))[i], ]
    transit.i <- c()
    for (j in 1:length(agent.i$actions)-1){
        transit.i[j] <- paste(agent.i$actions[j],
agent.i$actions[j+1], sep = '')
    }
    # for each subset of each agent, perform the 'pairing'
    transition[[i]] <- transit.i
}
transition
On Thu, Oct 20, 2011 at 8:52 AM, Sally Zhen <saliluna at gmail.com> wrote:

  
    
#
Here is another way of getting the lists
+     .all <- paste(.agent$actions, collapse = '')
+     .indx <- embed(seq(nchar(.all)), 2)
+     substring(.all, .indx[, 2], .indx[, 1])
+ })
$`2857`
[1] "LB"

$`293`
[1] "AD" "DF" "FH" "HN" "NA" "AC" "CH"

$`4596`
[1] "FG" "GN" "NH" "HH" "HK"

$`498`
[1] "FD" "DF" "FF" "FH" "HK"

$`5629`
[1] "HH" "HH" "HR"

$`6018`
[1] "SF" "FX" "XG" "GT"

$`6178`
[1] "XC" "CV"

$`6209`
[1] "FH" "HJ" "JU"

$`6847`
[1] "GD" "DD" "DB" "BN" "NK" "KO" "OV" "VS" "SS"

$`7535`
[1] "AE" "ED" "DC"

$`8562`
 [1] "KT" "TH" "HS" "SR" "RG" "GS" "SR" "RR" "RG" "GD" "DB" "BG"

        
On Thu, Oct 20, 2011 at 10:08 AM, Sally Zhen <saliluna at gmail.com> wrote: