Reputation:
I want to repeat again and again the given(below) sequence to fill approx 5000 rows in R.
Time data set:
8.00.00 AM
9.00.00 AM
10.00.00 AM
11.00.00 AM
12.00.00 PM
1.00.00 PM
2.00.00 PM
3.00.00 PM
4.00.00 PM
5.00.00 PM
6.00.00 PM
7.00.00 PM
8.00.00 PM
9.00.00 PM
Upvotes: 0
Views: 146
Reputation: 160417
There could be several reasons why you see blanks. I'll focus on two possibles: NA
s, and literal blanks.
srcvec <- c("8.00.00 AM", "9.00.00 AM", "10.00.00 AM", "11.00.00 AM", "12.00.00 PM",
"1.00.00 PM", "2.00.00 PM", "3.00.00 PM", "4.00.00 PM", "5.00.00 PM",
"6.00.00 PM", "7.00.00 PM", "8.00.00 PM", "9.00.00 PM", NA, ""
)
rep(srcvec, len=30)
# [1] "8.00.00 AM" "9.00.00 AM" "10.00.00 AM" "11.00.00 AM" "12.00.00 PM" "1.00.00 PM"
# [7] "2.00.00 PM" "3.00.00 PM" "4.00.00 PM" "5.00.00 PM" "6.00.00 PM" "7.00.00 PM"
# [13] "8.00.00 PM" "9.00.00 PM" NA "" "8.00.00 AM" "9.00.00 AM"
# [19] "10.00.00 AM" "11.00.00 AM" "12.00.00 PM" "1.00.00 PM" "2.00.00 PM" "3.00.00 PM"
# [25] "4.00.00 PM" "5.00.00 PM" "6.00.00 PM" "7.00.00 PM" "8.00.00 PM" "9.00.00 PM"
To remove the NA
s, we can simply use na.omit
:
rep(na.omit(srcvec), len=30)
# [1] "8.00.00 AM" "9.00.00 AM" "10.00.00 AM" "11.00.00 AM" "12.00.00 PM" "1.00.00 PM"
# [7] "2.00.00 PM" "3.00.00 PM" "4.00.00 PM" "5.00.00 PM" "6.00.00 PM" "7.00.00 PM"
# [13] "8.00.00 PM" "9.00.00 PM" "" "8.00.00 AM" "9.00.00 AM" "10.00.00 AM"
# [19] "11.00.00 AM" "12.00.00 PM" "1.00.00 PM" "2.00.00 PM" "3.00.00 PM" "4.00.00 PM"
# [25] "5.00.00 PM" "6.00.00 PM" "7.00.00 PM" "8.00.00 PM" "9.00.00 PM" ""
To remove blanks as well, we can filter on nzchar
, which returns true when strings continue 1 or more characters:
rep(Filter(nzchar, na.omit(srcvec)), len=30)
# [1] "8.00.00 AM" "9.00.00 AM" "10.00.00 AM" "11.00.00 AM" "12.00.00 PM" "1.00.00 PM"
# [7] "2.00.00 PM" "3.00.00 PM" "4.00.00 PM" "5.00.00 PM" "6.00.00 PM" "7.00.00 PM"
# [13] "8.00.00 PM" "9.00.00 PM" "8.00.00 AM" "9.00.00 AM" "10.00.00 AM" "11.00.00 AM"
# [19] "12.00.00 PM" "1.00.00 PM" "2.00.00 PM" "3.00.00 PM" "4.00.00 PM" "5.00.00 PM"
# [25] "6.00.00 PM" "7.00.00 PM" "8.00.00 PM" "9.00.00 PM" "8.00.00 AM" "9.00.00 AM"
If you have non-empty blanks (e.g., whitespace), you can use this:
srcvec <- c(srcvec, " ")
rep(Filter(function(a) !is.na(a) & nzchar(gsub("\\s", "", a)), srcvec), len=30)
# [1] "8.00.00 AM" "9.00.00 AM" "10.00.00 AM" "11.00.00 AM" "12.00.00 PM" "1.00.00 PM"
# [7] "2.00.00 PM" "3.00.00 PM" "4.00.00 PM" "5.00.00 PM" "6.00.00 PM" "7.00.00 PM"
# [13] "8.00.00 PM" "9.00.00 PM" "8.00.00 AM" "9.00.00 AM" "10.00.00 AM" "11.00.00 AM"
# [19] "12.00.00 PM" "1.00.00 PM" "2.00.00 PM" "3.00.00 PM" "4.00.00 PM" "5.00.00 PM"
# [25] "6.00.00 PM" "7.00.00 PM" "8.00.00 PM" "9.00.00 PM" "8.00.00 AM" "9.00.00 AM"
Upvotes: 1
Reputation: 297
First create a vector includes the data and then use matrix
x <- c("8.00.00 AM",
"9.00.00 AM",
"10.00.00 AM",
"11.00.00 AM",
"12.00.00 PM",
"1.00.00 PM",
"2.00.00 PM",
"3.00.00 PM",
"4.00.00 PM",
"5.00.00 PM",
"6.00.00 PM",
"7.00.00 PM",
"8.00.00 PM",
"9.00.00 PM")
output <- matrix(x,5000)
Upvotes: 0
Reputation: 181
You can use my onetree package from github yikeshu0611 to deal with problem.
devtools::install_github("yikeshu0611/onetree")
library(onetree)
df=read_Text("
time PAM
8.00.00 AM
9.00.00 AM
10.00.00 AM
11.00.00 AM
12.00.00 PM
1.00.00 PM
2.00.00 PM
3.00.00 PM
4.00.00 PM
5.00.00 PM
6.00.00 PM
7.00.00 PM
8.00.00 PM
9.00.00 PM
")
nrow(df)
358 ≈ 5000/14
df is your original data. Then we add the repeted number 358 to df.
df$n=358
Finally, we use flat_strech
to strech data by n
df2=flat_strech(data=df,strech="n")
nrow(df2)
5012
Upvotes: 0
Reputation: 704
Time = paste(c(8:12,1:9),".00.00",sep = "")
PM_AM <- paste(Time,rep(c("AM","PM"),c(4,10)))
n_rep <- 5000 %/% length(PM_AM)
n_remain <- 5000 %% length(PM_AM)
x <- rep(PM_AM,n_rep)
x <- c(x,PM_AM[1:n_remain])
Upvotes: 1