+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 16

Thread: paste unknown number of columns

  1. #1
    ggplot2orBust
    Points: 34,825, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,348
    Thanked 743 Times in 667 Posts

    paste unknown number of columns




    I have founf several close but not quite answers to this problem that I know is probably very simple.

    I want to take a list of equal length vectors (could be numeric or character) and paste them together using "." as the separator. the number of columns being passed is unknown.

    Here are my attempts:

    Code: 
    x <- CO2[, 1:3]
    y <- CO2[, 1:4]
    
    paste(x, sep=".")
    paste(y, sep=".")
    
    paste2 <- function(x) paste(x, sep=".") #attempt with do.call
    do.call("paste2", y)
    Thank you in advance!
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  2. #2
    Beep
    Points: 61,003, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,041
    Thanks
    260
    Thanked 2,135 Times in 1,816 Posts

    Re: paste unknown number of columns

    I don't quite get what you want. Can you provide a brief sample input and desired output?
    Morte a tutti i raptors
    001100010010011110100001101101110011

  3. #3
    ggplot2orBust
    Points: 34,825, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,348
    Thanked 743 Times in 667 Posts

    Re: paste unknown number of columns

    The x and the y were just showung the number of columns was unknown.

    I want the outcome for x to be:
    Code: 
    "Qn1.Quebec.nonchilled" "Qn1.Quebec.nonchilled" "Qn1.Quebec.nonchilled" ...
    and for y the output would have the same plus another dot and the 4th column. Note the length of the new vector will be = to length of the combined vectors.

    Maybe this is an mapply solution but I'm in the car now without R so can't check.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  4. #4
    Phineas Packard
    Points: 9,313, Level: 64
    Level completed: 88%, Points required for next Level: 37
    Lazar's Avatar
    Location
    Sydney
    Posts
    905
    Thanks
    148
    Thanked 246 Times in 223 Posts

    Re: paste unknown number of columns

    Code: 
    dat<-data.frame(x=rnorm(10), y=letters[1:10])
    
    cat(as.character(dat$x),sep=".", "\n")
    cat(as.character(dat$y),sep=".",  "\n")
    is this what you mean.

    OR
    Code: 
    f1<-function(x) cat(as.character(x),sep=".",  "\n")
    f2<- colwise(f1)
    
    f2(dat)
    Last edited by Lazar; 03-12-2012 at 05:56 PM. Reason: forgot new line command

  5. #5
    ggplot2orBust
    Points: 34,825, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,348
    Thanked 743 Times in 667 Posts

    Re: paste unknown number of columns

    @Lazar I can't check this yet but the use of cat doesn't't seem right. I want to combine predictors (row by row) to make a final vector that is a character string of the predictors pasted together.

    This may also be q job for reduce.

    I would have thought I could have just used paste though.


    One more problem is that if any calls in the paste are NA then it needs to return NA.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  6. #6
    Beep
    Points: 61,003, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,041
    Thanks
    260
    Thanked 2,135 Times in 1,816 Posts

    Re: paste unknown number of columns

    Do either of these do what you want?

    Code: 
    
    x <- CO2[, 1:3]
    y <- CO2[, 1:4]
    
    apply(x, 1, paste, collapse = ".")
    apply(x, 1, function(x){if(any(is.na(x))){NA}else{paste(x, collapse = ".")}})
    Morte a tutti i raptors
    001100010010011110100001101101110011

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

    trinker (03-12-2012)

  8. #7
    ggplot2orBust
    Points: 34,825, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,348
    Thanked 743 Times in 667 Posts

    Re: paste unknown number of columns

    @Dason I was just about to write that I got the apply and was going to work on NA. when apply came to me I was like "You bone head". That saves me from figuring out the NA problem. Much appreciated.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  9. #8
    Beep
    Points: 61,003, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,041
    Thanks
    260
    Thanked 2,135 Times in 1,816 Posts

    Re: paste unknown number of columns

    No problem. Sorry about the horribly formatted code. When I write code here and don't actually check it within R I tend to just write gross one-liners.
    Morte a tutti i raptors
    001100010010011110100001101101110011

  10. #9
    Probably A Mammal
    Points: 18,660, Level: 86
    Level completed: 62%, Points required for next Level: 190
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    2,194
    Thanks
    291
    Thanked 491 Times in 446 Posts

    Re: paste unknown number of columns

    Just looking at the OP, you say you want to use a list of vectors but then refer to columns. Lists don't have columns! You don't provide an example of the input or the output! Bad form!

  11. #10
    ggplot2orBust
    Points: 34,825, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,348
    Thanked 743 Times in 667 Posts

    Re: paste unknown number of columns

    I was doing it from my phone. Geesh cut a guy a break BG And technically a data frame is a list of equal length vectors. So CO2 is an example of the input. I took care of the output when dason asked. There lies your criticisms in a pile of rubbish. I hypothesize this post is just a red herring and you're playing both the bots and raptors.

    Is your Monday that hard that you're picking on poor trinker now? And besides my teeth are 4 inches long. I ask is it wise to pick fights with raptors. That makes me wonder why would you not be afraid? Unless....That's it!!!!!!!

    We're all worried about bots and raptors meanwhile who's looking out for Wepwawet the one who ushers the dead into hell. My gosh bots and raptors may take your life but Wepwawet takes your soul. Beware TSers he's a snake in the grass.
    Last edited by trinker; 03-13-2012 at 06:11 AM. Reason: I can't spell soul
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  12. #11
    Probably A Mammal
    Points: 18,660, Level: 86
    Level completed: 62%, Points required for next Level: 190
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    2,194
    Thanks
    291
    Thanked 491 Times in 446 Posts

    Re: paste unknown number of columns

    Tall tales.

    Btw, you could have did the paste thing quite easily from do.call if you understand that you need to supply it a list object that contains all the parameters you want to use. This includes the "sep" parameter.

    Code: 
    set.seed(100)
    df <- data.frame(A = runif(10, 0, 10), B = runif(10, 0, 10), C = runif(10, 0, 10)) 
    y <- as.list(df)  # make it a list
    y$sep = "-Wepwawet-"  # set our separator
    do.call("paste", y)
    #  [1] "3.07766110869125-Wepwawet-6.24996477039531-Wepwawet-5.35811153938994"
    #  [2] "2.57672501029447-Wepwawet-8.82165518123657-Wepwawet-7.10803845431656"
    #  [3] "5.52322433330119-Wepwawet-2.8035383997485-Wepwawet-5.38348698290065" 
    #  [4] "0.563831503968686-Wepwawet-3.98487901547924-Wepwawet-7.4897222686559"
    #  [5] "4.68549283919856-Wepwawet-7.6255108229816-Wepwawet-4.20101450523362" 
    #  [6] "4.83770735096186-Wepwawet-6.69021712383255-Wepwawet-1.71420212602243"
    #  [7] "8.12402617651969-Wepwawet-2.04612161964178-Wepwawet-7.70301609765738"
    #  [8] "3.70320537127554-Wepwawet-3.57524853432551-Wepwawet-8.81953587755561"
    #  [9] "5.46558595029637-Wepwawet-3.59475114848465-Wepwawet-5.49096710281447"
    # [10] "1.70262051047757-Wepwawet-6.90290528349578-Wepwawet-2.77723756618798"
    The reason your paste2 fails is because you don't pass anything else that paste requires ... like the stuff to paste! You'd want to do something like

    Code: 
    mypaste <- function(..., sep = ".") {paste(..., sep = sep)}

  13. The Following User Says Thank You to bryangoodrich For This Useful Post:

    trinker (03-13-2012)

  14. #12
    ggplot2orBust
    Points: 34,825, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,348
    Thanked 743 Times in 667 Posts

    Re: paste unknown number of columns

    Very helpful and informative. Why didn't my paste2 work then? This helps me understand do.call a bit better. I only started using it recently when you explained it about a month back.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  15. #13
    Beep
    Points: 61,003, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,041
    Thanks
    260
    Thanked 2,135 Times in 1,816 Posts

    Re: paste unknown number of columns

    But really... this is a situation that is probably more appropriate for apply.

    do.call is nice and can help in certain situations but for readability purposes I would opt for the apply method in this case.
    Morte a tutti i raptors
    001100010010011110100001101101110011

  16. #14
    Probably A Mammal
    Points: 18,660, Level: 86
    Level completed: 62%, Points required for next Level: 190
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    2,194
    Thanks
    291
    Thanked 491 Times in 446 Posts

    Re: paste unknown number of columns

    Quote Originally Posted by trinker View Post
    Why didn't my paste2 work then?
    Didn't you read the bottom of my comment?! Compare my wrapper to yours.

    @Dason, I agree that the semantics for using apply are better, but this approach could offer speed boosts, and it is good that Trinker understands how to utilize it. For if it is used inside a function that needs to be fast, then its obscure approach may still be the better choice, because functions in one's package don't necessarily need to be the most comprehensible code; they need to be efficient!

  17. #15
    ggplot2orBust
    Points: 34,825, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,933
    Thanks
    1,348
    Thanked 743 Times in 667 Posts

    Re: paste unknown number of columns


    Oh!!!!!!!!!!! I get it. the sep=sep sets up "hey I'm going to send you something called sep" and the y$sep is "that thing I told you I was going to send you. It was part of the list I'm giving you do.call because you only seem to like list stuff" (do.call is a list snob). Thanks bryangoodrich. Even clearer. You're still a Wepwawet.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

+ Reply to Thread
Page 1 of 2 1 2 LastLast

           




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