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 8250505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:54:47+00:00 2026-06-07T23:54:47+00:00

I have a matrix called (b2) that contains 3565 rows and 125 columns with

  • 0

I have a matrix called (b2) that contains 3565 rows and 125 columns with only dichotomous values (0 and 1)

I designed a function to compare row i and row i+1 and store the number of differences in a new vector.

loopPhudcf <- function(x){
  ## create a vector to store the results of your for loop
  output <- as.vector(rep(0, length(x[,1])))
  for (i in 1:(nrow(x))-1)  {
    output[i]<-as.vector(table(x[i,]==x[i+1,]))[1]
  }
  a<-nrow(x)
  b<-nrow(x)-1
  output<-t(as.matrix(output[c(a,1:b)]))
  output[output==ncol(x)]<-0
  return(output)
}

phudcfily123<-loopPhudcf(b2)

The function works fine, but I also have an ID variable which I added to my original matrix using: b2<-transform(b2,id=a$id), then resulting in a 3565 by 126 being the last one the id variable

I wanted to apply my function using ddply {plyr} but to do this i need to subset only my original matrix without the ID variable (as.matrix(b2[,1:(ncol(b2)-1)])) but it keeps saying that my function is not a function 🙁

x <- ddply(.data = b2, .var = c("id"), .fun = loopPhudcf(as.matrix(b2[,1:(ncol(b2)-1)])))

Error in llply(.data = .data, .fun = .fun, ..., .progress = .progress,  : 
  .fun is not a function.

can anyone help me overcome this issue?

  • 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-06-07T23:54:49+00:00Added an answer on June 7, 2026 at 11:54 pm

    Thank you,
    using the package reshape i was able to get the same result reached using Brian’s method, this is the code:

    x<-sparseby(as.matrix(b2[,1:125]),list(group = b2[,126]), function(subset) loopPhudcf(as.matrix(b2[,1:125])))
    

    Something a little bit strange to me was that using this approach and the approach kindly suggested by Brian I obtained a new matrix instead of my desired vector

    dim(x)
    [1]  155 3566
    

    So, I only had to subset the first row to get the vector since the rows contained the same information. My vector with a length of 3565 was obtained using:

    x1<-x[1,2:ncol(x)]
    

    I started with 2 given that The first column accounts for the id variable in b2.
    Thank you again!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix of data (the columns represent time, and the rows spectrum
I have a matrix of objects that contains data in this form: name A,2,name
I have a matrix called matrix that looks like this: charge convexhull_xCoor convexhull_yCoor id
Have a matrix report now that has Position, Hours and Wages for a location
I have a class called forest and a property called fixedPositions that stores 100
Let's suppose I have a numpy matrix variable called MATRIX with 3 coordinates: (x,
I have a boolean[][] 2D-array called matrix , which encodes a directed graph such
I am using a class called BigNumDesc that represents a number. I have a
I have canvas called drawCanvas to show images and inkcanvas that is contained in
I have a 101x82 size matrix called A . Using this variable matrix, I

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.