Home > database >  Problem pasting 3 strings with different separator
Problem pasting 3 strings with different separator

Time:01-13

Probably it´s a nonsense but I´ve been playing around with paste and paste0 with no success.

My data frame looks like this

stck <-structure(list(haul = 1:11,
         year = c(1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L)),
         row.names = c(NA, 11L), class = "data.frame")

and I´m trying to generate a code like this: N83_01, N83_02, ...., N83_10

Namely, I want to create a code pasting the two last number of the year plus the haul number (in "two-number" format).

Hitherto, I couldn´t find the way to paste "N". This is what I have

stck$cod_lance <- as.factor(ifelse (stck$lance >=1 & stck$lance <10, paste(str_sub(stck$fecha, 3, 4), stck$lance, sep="_0"),
                                    paste(str_sub(stck$fecha, 3, 4), stck$lance, sep="_")))

How can I get the right output?. Thanks in advance.

CodePudding user response:

Use paste0 to avoid separators. Pass "N" as the first argument, then the last two digits of stck$year using substr or stck$year %% 100. Next comes an underscore, and finally the haul number. You can pad the haul number with a leading zero in the case of single digits using sprintf:

paste0('N', substr(stck$year, 3, 4), "_", sprintf('d', stck$haul))
#>  [1] "N83_01" "N83_02" "N83_03" "N83_04" "N83_05" "N83_06" "N83_07"
#>  [8] "N83_08" "N83_09" "N83_10" "N83_11

CodePudding user response:

You can also combine all with sprintf

sprintf('N%1$d_%2$02d', stck$year %% 100, stck$haul)

# [1] "N83_01" "N83_02" "N83_03" "N83_04" "N83_05" "N83_06"
# "N83_07" "N83_08" "N83_09" "N83_10" "N83_11"

CodePudding user response:

Definitely go with Allan's answer, but to complete your existing code, we can add another paste to prefix with N:

library(stringr)

as.factor(
  paste0("N",
  ifelse(stck$haul >= 1 & stck$haul < 10,
          paste(str_sub(stck$year, 3, 4), stck$haul, sep="_0"),
          paste(str_sub(stck$year, 3, 4), stck$haul, sep="_")))
  )

#  [1] N83_01 N83_02 N83_03 N83_04 N83_05 N83_06 N83_07 N83_08 N83_09 N83_10 N83_11 N83_12
# Levels: N83_01 N83_02 N83_03 N83_04 N83_05 N83_06 N83_07 N83_08 N83_09 N83_10 N83_11 N83_12

CodePudding user response:

Here is a an alternative approach using formatC and some tidyverse helper:

library(dplyr)
library(stringr)
stck %>% 
  mutate(x = paste0("N", str_sub(year, -2), "_", formatC(haul, width=2, flag="0"))) %>% 
  pull(x)
"N83_01" "N83_02" "N83_03" "N83_04" "N83_05" "N83_06" "N83_07" "N83_08" "N83_09" "N83_10" "N83_11"
  • Related