Powege
Powege

Reputation: 715

Count characters in sequence, ignoring given character (R)

I have a vector of single character strings:

seq <- c("c","t","c","t","c","c","t","a","t", "g", "g", "c", "g", "g", "g", "a", "a", "g", "c", "-", "-", "a", "a","-", "-", "-", "c", "t", "g")

I would like to create a vector of equal length that counts the strings from a starting integer but ignores the "-".

For example, if the starting integer is 1000

The output string would be:

out <-c(1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, NA, NA, 1019, 1020, NA, NA, NA, 1021, 1022, 1023)

Any help would be very much appreciated!

Upvotes: 2

Views: 169

Answers (1)

Roland
Roland

Reputation: 132706

This is simple:

res <- rep(NA_integer_, length(seq))
res[seq != "-"] <- seq(from = 1000, by = 1, length.out = sum(seq != "-"))

Upvotes: 5

Related Questions