user2004820
user2004820

Reputation: 321

pasting x number of strings to y number of strings using sapply

I have two vectors, vector "t" is times, vector "d" is dates.
I want to extract data for all times in t for all dates in d
I know how to format the dates to POSIXct from character so this isn't the issue, I tried to use sapply to go over the days one by one the code I used was as follows:

    t <- c("11:25:30", "12:00:00", "15:31:25", "19:15:48", "23:51:06")
    d <- c("2013-01-17", "2013-01-24", "2013-02-09", "2013-02-11")  
    c1 <- sapply(d, function(i) paste(d[i],t))

I expected to see a list if used lapply or a vector when using sapply with the strings " "2013-01-17 11:25:30", "2013-01-17 12:00:00", 2013-01-17 15:31:25" ... al the way to the end combining each date with all times in t.

Upvotes: 0

Views: 77

Answers (3)

Didzis Elferts
Didzis Elferts

Reputation: 98449

You can combine expand.grid() with apply(). expand.grid() will make data frame of all posible combinations of d and t. The with apply() function paste() is applied to each row of this data frame. collapse=" " ensures that string are put together and space put between them.

apply(expand.grid(d,t),1,paste,collapse=" ")

 [1] "2013-01-17 11:25:30" "2013-01-24 11:25:30" "2013-02-09 11:25:30"
 [4] "2013-02-11 11:25:30" "2013-01-17 12:00:00" "2013-01-24 12:00:00"
 [7] "2013-02-09 12:00:00" "2013-02-11 12:00:00" "2013-01-17 15:31:25"
[10] "2013-01-24 15:31:25" "2013-02-09 15:31:25" "2013-02-11 15:31:25"
[13] "2013-01-17 19:15:48" "2013-01-24 19:15:48" "2013-02-09 19:15:48"
[16] "2013-02-11 19:15:48" "2013-01-17 23:51:06" "2013-01-24 23:51:06"
[19] "2013-02-09 23:51:06" "2013-02-11 23:51:06"

Upvotes: 2

Tyler Rinker
Tyler Rinker

Reputation: 109874

This is the fastest way and has the added benefit of the least amount of typing:

c(outer(d, t, paste))

##  [1] "2013-01-17 11:25:30" "2013-01-24 11:25:30" "2013-02-09 11:25:30"
##  [4] "2013-02-11 11:25:30" "2013-01-17 12:00:00" "2013-01-24 12:00:00"
##  [7] "2013-02-09 12:00:00" "2013-02-11 12:00:00" "2013-01-17 15:31:25"
## [10] "2013-01-24 15:31:25" "2013-02-09 15:31:25" "2013-02-11 15:31:25"
## [13] "2013-01-17 19:15:48" "2013-01-24 19:15:48" "2013-02-09 19:15:48"
## [16] "2013-02-11 19:15:48" "2013-01-17 23:51:06" "2013-01-24 23:51:06"
## [19] "2013-02-09 23:51:06" "2013-02-11 23:51:06"

Upvotes: 5

Paul Hiemstra
Paul Hiemstra

Reputation: 60944

You can use expand.grid and paste like this:

t <- c("11:25:30", "12:00:00", "15:31:25", "19:15:48", "23:51:06")
d <- c("2013-01-17", "2013-01-24", "2013-02-09", "2013-02-11") 
do.call("paste", expand.grid(d,t))
 [1] "2013-01-17 11:25:30" "2013-01-24 11:25:30" "2013-02-09 11:25:30"
 [4] "2013-02-11 11:25:30" "2013-01-17 12:00:00" "2013-01-24 12:00:00"
 [7] "2013-02-09 12:00:00" "2013-02-11 12:00:00" "2013-01-17 15:31:25"
[10] "2013-01-24 15:31:25" "2013-02-09 15:31:25" "2013-02-11 15:31:25"
[13] "2013-01-17 19:15:48" "2013-01-24 19:15:48" "2013-02-09 19:15:48"
[16] "2013-02-11 19:15:48" "2013-01-17 23:51:06" "2013-01-24 23:51:06"
[19] "2013-02-09 23:51:06" "2013-02-11 23:51:06"

I haven't found a way to tweak the sep parameter of paste using this syntax, but it gives the output you need.

Upvotes: 2

Related Questions