+ Reply to Thread
Results 1 to 8 of 8

Thread: Efficiently combining String and Number Arrays

  1. #1
    Points: 3,609, Level: 37
    Level completed: 73%, Points required for next Level: 41

    Location
    Norman, OK 73019
    Posts
    100
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Efficiently combining String and Number Arrays



    I have two big matrices: A and B. A contains all numbers and B contains strings. For purpose of writing to a file I do cbind on A and B and use 'write' command to push it into a csv file. It works fine but in csv file I see that it writes A matrix first and then starts writing B after right hand bottom cell.

    How can I do it in a cleaner way?

    Thanks

  2. #2
    FormerlyKnownAsRaptor
    Points: 24,451, Level: 95
    Level completed: 11%, Points required for next Level: 899
    Awards:
    Activity Award
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,174
    Thanks
    884
    Thanked 552 Times in 500 Posts

    Re: Efficiently combining String and Number Arrays

    Sample code could be helpful here and also your desired outcome.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

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

    sak (03-05-2013)

  4. #3
    Points: 616, Level: 12
    Level completed: 32%, Points required for next Level: 34

    Location
    US
    Posts
    26
    Thanks
    1
    Thanked 8 Times in 8 Posts

    Re: Efficiently combining String and Number Arrays

    Can you post an abbreviated example of the code you have, the output you want, and the output you currently get?
    Edit: not fast enough, trinker beat me to it

  5. #4
    Points: 3,609, Level: 37
    Level completed: 73%, Points required for next Level: 41

    Location
    Norman, OK 73019
    Posts
    100
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Re: Efficiently combining String and Number Arrays

    A very simple example that I simulated is:

    Code: 
    A = c('a','a','b','a','c')
    B = c(1,3,4,5,6)
    D = cbind(A,B)
    
    > D
         A   B  
    [1,] "a" "1"
    [2,] "a" "3"
    [3,] "b" "4"
    [4,] "a" "5"
    [5,] "c" "6"
    
    write(D,"C:\\OutTest.csv")
    
    When I open the csv, it looks as following:
    a
    a
    b
    a
    c
         1
         3
         4
         5
         6
    Sorry I think I lied earlier. The 1,3,4,5,6 are not printed starting in right-bottom cell but in the same column i.e. B is printed after A.

  6. #5
    FormerlyKnownAsRaptor
    Points: 24,451, Level: 95
    Level completed: 11%, Points required for next Level: 899
    Awards:
    Activity Award
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,174
    Thanks
    884
    Thanked 552 Times in 500 Posts

    Re: Efficiently combining String and Number Arrays

    Try this method for writing to a file instead:

    Code: 
    write.table(D,"OutTest.csv", sep = ",", col.names = T, row.names=F, qmethod = "double")
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  7. #6
    Points: 3,609, Level: 37
    Level completed: 73%, Points required for next Level: 41

    Location
    Norman, OK 73019
    Posts
    100
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Re: Efficiently combining String and Number Arrays

    Cool. It works.

    Thanks

  8. #7
    Probably A Mammal
    Points: 14,507, Level: 78
    Level completed: 15%, Points required for next Level: 343
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    1,956
    Thanks
    223
    Thanked 419 Times in 387 Posts

    Re: Efficiently combining String and Number Arrays

    Matrices in R only hold one data type. You want to use a data.frame data type. It's essentially a list of equal length vectors that displays like a matrix. The nice part about that is that the vectors can now be different data types. If you have separate numeric and character vectors A and B, then simply do

    Code: 
    # You can have characters converted to factor data type if you prefer; defaults to TRUE
    x <- data.frame(nums = A, strings = B, stringsAsFactors = FALSE)
    
    # write.csv is just a wrapper for write.table with some select defaults, like sep = ","
    write.csv(x, "Output.csv", row.names = FALSE)

  9. The Following 2 Users Say Thank You to bryangoodrich For This Useful Post:

    Dason (08-19-2012), sak (08-20-2012)

  10. #8
    Points: 3,609, Level: 37
    Level completed: 73%, Points required for next Level: 41

    Location
    Norman, OK 73019
    Posts
    100
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Re: Efficiently combining String and Number Arrays


    Simple yet very insightful answer especially good for a novice users like.

+ 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