Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:15:42+00:00 2026-05-30T00:15:42+00:00

I would like to convert a vector into a transitions matrix first (which I

  • 0

I would like to convert a vector into a transitions matrix first (which I managed). As a second step I would like apply the resulting function to a dataset where different respondents did different tasks.
As a result I would like to get a List which is nested on Respondent and Task.

Here is an example data frame:

  Data <- data.frame(
    respondent = c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2),
    task = c(1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,2,2,2,2,2),
    acquisition = sample(1:5, replace = TRUE)
    )

and here my result vector and function that takes the acquisition vector and generates a transition matrix:

result <- matrix(data = 0, nrow = 5, ncol = 5)

gettrans <- function(invec){
            for (i in 1:length(invec)-1){
            result[invec[i],invec[i+1]] <- result[invec[i], invec[i+1]] + 1
            }
            return(result)
          }

Now, I get a flattened result with

with(Data,aggregate(acquisition,by=list(respondent=respondent,task=task),gettrans))

However what I would like would look something like:

$respondent
[1]$task[1]
result

$respondent
[1]$task[2]
result 

...

I played around with dlply but could not get that to work …
Any suggestions appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:15:43+00:00Added an answer on May 30, 2026 at 12:15 am

    dlply naturally gives you a list (rather than a list of lists). The standard way of calling it would be

    (ans_as_list <- dlply(
      Data, 
      .(respondent, task), 
      summarise, 
      res = gettrans(acquisition)
    ))
    

    This should be suitable for most purposes, but if you really must have a list of lists, use llply (or equivalently, lapply) to restructure.

    (ans_as_list_of_lists <- llply(levels(factor(Data$respondent)), function(lvl)
    {
      ans_as_list[grepl(paste("^", lvl, sep = ""), names(ans_as_list))]
    }))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MxN Matrix and would like to convert into a vector MNx1
I'm having a for loop which I would like to convert into parallel_for fnc
I would like to convert a string into a node. I have a method
I would like to convert some value from a XML attribute into valid HTML
I would like to be able to convert between std::vector and its underlying C
I would like to convert an Int32 in the range 0-15 into a the
I would like to convert an integer [] [] to a Vector<Vector<Double>> . After
I would like to convert a string of delimited dimension values into floating numbers.
I would like to convert object (which holds a string) to double and if
I would like to convert tab to spaces in gVim. I added the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.