+ Reply to Thread
Results 1 to 5 of 5

Thread: do.call from apply function

  1. #1
    Points: 1,936, Level: 26
    Level completed: 36%, Points required for next Level: 64

    Posts
    79
    Thanks
    31
    Thanked 1 Time in 1 Post

    do.call from apply function



    Dear All,

    I would like to cluster each column of below matrix. Then I want to extract cluster vectors as column matrix. Thank you

    PHP Code: 
     df data.frame(id=c(3,5,1,6,8,2,3,5), val=c(14,17,11,19,12,13,17,16))
    ss=apply(df2, function(xkmeans(x,3))
    ff=do.call(c,ss

    $val.cluster
    [12 3 2 1 2 2 3 3
     $id
    .cluster
    [12 3 1 3 3 1 2 3

    But the resulst should look like
    $val
    .cluster   $id.cluster
    2                      2
    3                      3
    2                      1
    1                      3
    2                      3
    2                      1
    3                      2
    3                      3 
    Last edited by masarimk; 07-18-2012 at 05:33 AM.

  2. #2
    RotParaTon
    Points: 46,173, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,067
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: do.call from apply function

    It sounds more like you want something like this

    Code: 
    
    do.call(cbind, sapply(ss, "[", 1))
    The sapply(ss, "[", 1) grabs the first element from each part of the list called ss. Then we want to bind those together as columns so we use cbind. But we want to bind the elements of a list together so we need to use do.call so those get passed as the parameters to cbind.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  3. The Following User Says Thank You to Dason For This Useful Post:

    masarimk (07-18-2012)

  4. #3
    Points: 1,936, Level: 26
    Level completed: 36%, Points required for next Level: 64

    Posts
    79
    Thanks
    31
    Thanked 1 Time in 1 Post

    Re: do.call from apply function

    Thank you very much. I really appreciate that. One more question:
    When I run below code , it gives me X1 and X2 not the variable names,Is there a way that I can put the names of the variables for the center?

    Code: 
    df = data.frame(id=c(3,5,1,6,8,2,3,5), val=c(14,17,11,19,12,13,17,16))
    ss=apply(df, 2, function(x) kmeans(x,3))
    data.frame(do.call(cbind, sapply(ss, "[", 7)),do.call(cbind, sapply(ss, "[", 2)))
      id.size val.size       X1    X2
    1       3        2 5.333333 11.50
    2       4        4 2.250000 17.25
    3       1        2 8.000000 13.50
    Last edited by masarimk; 07-18-2012 at 10:29 AM.

  5. #4
    FormerlyKnownAsRaptor
    Points: 24,320, Level: 94
    Level completed: 97%, Points required for next Level: 30
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,165
    Thanks
    882
    Thanked 547 Times in 495 Posts

    Re: do.call from apply function

    Not sure if this is what you're after or if you're looking to automate this somehow:

    Code: 
    df = data.frame(id=c(3,5,1,6,8,2,3,5), val=c(14,17,11,19,12,13,17,16)) 
    ss=apply(df, 2, function(x) kmeans(x,3)) 
    dat <- data.frame(do.call(cbind, sapply(ss, "[", 7)),do.call(cbind, sapply(ss, "[", 2))) 
    names(dat)[3:4] <-c("new.name1", "new.name2")
    dat
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  6. #5
    Points: 1,936, Level: 26
    Level completed: 36%, Points required for next Level: 64

    Posts
    79
    Thanks
    31
    Thanked 1 Time in 1 Post

    Re: do.call from apply function


    Thank you Sir. I really appreciate that.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats