Constructing the transition pair using loop function and paste()
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:
Hi all,
I'd like to thank those who helped me with my previous loop function
question with agents/events. I have solved the problem with the advice from
this community.
I have now moved on to the next step, which requires me to find all the
transition pair within an event. A sample data and the R commands I've
written are as follow:
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
for (i in 1:length(unique(mydata$agents))){ ? ? ? ? # decompose the data
frame by agents
agent.i <- mydata[mydata$agents == (unique(mydata$agents))[i], ]
transition <- vector('list', length(unique(mydata$agents))) ? ? ? ?# create
a list to hold the outputs
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
The actions are ordered, so what I need to do is just to paste each action
to the next one to form a pair.
My attempt only produced the desired result for the last agent:
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
[[4]]
NULL
[[5]]
NULL
[[6]]
NULL
[[7]]
NULL
[[8]]
NULL
[[9]]
NULL
[[10]]
NULL
[[11]]
[1] "AE" "ED" "DC"
Is there any way to avoid using 2-level loop function, although to me it's
the most intuitive method? Any pointer would be greatly appreciated!
Regards,
Sally
? ? ? ?[[alternative HTML version deleted]]
______________________________________________ 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve?