How Create a Sequence of Strings with Different Numbers in R

how create a sequence of strings with different numbers in R

The sprintf function should also work:

rasters <- sprintf("raster[%s]",seq(1:99))
head(rasters)
[1] "raster[1]" "raster[2]" "raster[3]" "raster[4]" "raster[5]" "raster[6]"

As suggested by Richard Scriven, %d is more efficient than %s. So, if you were working with a longer sequence, it would be more appropriate to use:

rasters <- sprintf("raster[%d]",seq(1:99))

Creating sequence of numbers inside a string R

The following solution needs only one *apply loop.

mapply(function(x, y) paste(x:y, collapse = ","), df$start, df$end)
#[1] "2,3,4,5" "4,5,6,7" "6,7,8,9" "8,9,10,11" "10,11,12,13"

With the new lambdas, same output.

mapply(\(x, y) paste(x:y, collapse = ","), df$start, df$end)

How to create a sequence starting with a character and then with numbers in R

There's ::

1998:2011

And ' or " to create string constants:

'y'

And paste0 to concatenate both:

paste0('y', 1998:2011)

Note how the paste0 function is applied to the second parameter, element by element. That's one of R's strengths.

sequence of strings of letters in R

Here is a solution using dplyr (not necessary, just a preference for its syntax). It can almost certainly be simplified and made more readable, but at least it gives the expected output:

library(dplyr)
res <-
GRE %>%
group_by(Trial, Subject) %>%
mutate(
pret1 = grepl("_pret1$", LABEL),
t1 = grepl("_t1$", LABEL),
pret2 = grepl("_pret2$", LABEL),
t2 = grepl("_t2$", LABEL),
seq_ = (any(pret1) & any(t1) & (pret1 | t1)) |
(any(pret2) & any(t2) & (pret2 | t2)),
no_seq_ = ((all(!pret1) | all(!t1)) & (pret1 | t1)) |
((all(!pret2) | all(!t2)) & (pret2 | t2)),
OUTPUT_2 = ifelse(seq_, 0L, ifelse(no_seq_, 1L, NA_integer_))
) %>%
ungroup() # %T>% print(n = 28)

# # A tibble: 28 × 11
# LABEL Subject Trial OUTPUT pret1 t1 pret2 t2 seq_ no_seq_ OUTPUT_2
# <fctr> <dbl> <int> <fctr> <lgl> <lgl> <lgl> <lgl> <lgl> <lgl> <int>
# 1 vc 1 1 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 2 gfda 1 1 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 3 gsgs_pret2 1 1 0 FALSE FALSE TRUE FALSE TRUE FALSE 0
# 4 sfgsgt_pret2 1 1 0 FALSE FALSE TRUE FALSE TRUE FALSE 0
# 5 hhjcf_t2 1 1 0 FALSE FALSE FALSE TRUE TRUE FALSE 0
# 6 xa_postt2 1 1 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 7 sgs 1 1 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 8 sgsd 1 2 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 9 fgnx_pret1 1 2 0 TRUE FALSE FALSE FALSE TRUE FALSE 0
# 10 wqraffsd_pret1 1 2 0 TRUE FALSE FALSE FALSE TRUE FALSE 0
# 11 zdgn_t1 1 2 0 FALSE TRUE FALSE FALSE TRUE FALSE 0
# 12 with_postt1 1 2 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 13 nzf 1 2 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 14 great_postt2 1 2 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 15 l 2 3 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 16 fjs 2 3 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 17 ssmlk_t1 2 3 1 FALSE TRUE FALSE FALSE FALSE TRUE 1
# 18 gjkgj_t1 2 3 1 FALSE TRUE FALSE FALSE FALSE TRUE 1
# 19 djdj_postt1 2 3 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 20 ityufhj 2 3 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 21 eyhjjfjfhjf 2 3 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 22 dghjdj_pret2 2 4 0 FALSE FALSE TRUE FALSE TRUE FALSE 0
# 23 gjkt_t2 2 4 0 FALSE FALSE FALSE TRUE TRUE FALSE 0
# 24 kuutt_t2 2 4 0 FALSE FALSE FALSE TRUE TRUE FALSE 0
# 25 truetye_postt2 2 4 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 26 fj 2 4 NA FALSE FALSE FALSE FALSE FALSE FALSE NA
# 27 hgfg_pret1 2 4 1 TRUE FALSE FALSE FALSE FALSE TRUE 1
# 28 zetytu 2 4 NA FALSE FALSE FALSE FALSE FALSE FALSE NA

identical(as.integer(as.character(res$OUTPUT)), res$OUTPUT_2)
# [1] TRUE
# Warning message:
# In identical(as.integer(as.character(res$OUTPUT)), res$OUTPUT_2) :
# NAs introduced by coercion

Generate Sequence of Numbers in R with two digit

Yes, specify %02d

sprintf("out%02d", 1:30)
#[1] "out01" "out02" "out03" "out04" "out05" "out06" "out07" "out08" "out09"....

R - generate a sequence of letters and numbers

You can do

paste0(rep(LETTERS[1:5], 3), rep(1:3, each = 5))
#> [1] "A1" "B1" "C1" "D1" "E1" "A2" "B2" "C2" "D2" "E2" "A3" "B3" "C3" "D3" "E3"

generate sequence of numbers in R according to other variables

Here's one using ave:

OUT <- within(IN, {N <- ave(ID, list(ID, DATE), FUN=seq_along)})

Create sequence of repeated values, in sequence?

You missed the each= argument to rep():

R> n <- 3
R> rep(1:5, each=n)
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
R>

so your example can be done with a simple

R> rep(1:8, each=20)

How to create a sequence numerical column based on two columns in r?

This is a perfect use case for the rleid function from the data.table package:

# example data
xx <- rep(Sys.Date(), 5)
xx <- c(xx, xx + lubridate::days(1))
id <- rep(c(1:4), c(2,3,3,2))
dat <- data.frame(date = xx, id = id)

# date id
# 1 2021-03-29 1
# 2 2021-03-29 1
# 3 2021-03-29 2
# 4 2021-03-29 2
# 5 2021-03-29 2
# 6 2021-03-30 3
# 7 2021-03-30 3
# 8 2021-03-30 3
# 9 2021-03-30 4
# 10 2021-03-30 4

library(data.table)
dat_dt <- as.data.table(dat)
dat_dt[,target_id := rleid(date, id)]

# date id target_id
# 1: 2021-03-29 1 1
# 2: 2021-03-29 1 1
# 3: 2021-03-29 2 2
# 4: 2021-03-29 2 2
# 5: 2021-03-29 2 2
# 6: 2021-03-30 3 3
# 7: 2021-03-30 3 3
# 8: 2021-03-30 3 3
# 9: 2021-03-30 4 4
#10: 2021-03-30 4 4

And here's how you could use %>% and mutate to solve it:

library(tidyverse)
dat %>%
mutate(target_id = data.table::rleid(date, id))


Related Topics



Leave a reply



Submit