MFR
MFR

Reputation: 2077

Select the last n characters in a string

I have the following dataset

df <-data.frame(fact=c("a,bo,v", "c,b,v,d", "c"))

I wish to select the last two items for each row. So, Ideally I wish to have this output:

    fact
1    bo,v
2    v,d
3    c

I've tried to split the rows and then choose the last two items:

spl <- strsplit(as.character(df$fact), split = ",")

tail(spl[[1]], n=2)

But doe not give me the correct results

Upvotes: 1

Views: 829

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389235

We can use sapply to loop over every element of fact, split it on basis of , and then select the last n elements using tail

n <- 2

sapply(as.character(df$fact), function(x) {
       temp = unlist(strsplit(x, ','))
       tail(temp, n)
}, USE.NAMES = F)

#[[1]]
#[1] "bo" "v" 

#[[2]]
#[1] "v" "d"

#[[3]]
#[1] "c"

A better option with dplyr I feel using rowwise

library(dplyr)
df %>%
 rowwise() %>%
 mutate(last_two = paste0(tail(unlist(strsplit(as.character(fact),",")), n), 
                                                                collapse = ","))

#     fact last_two
#   <fctr>    <chr>
#1  a,bo,v     bo,v
#2 c,b,v,d      v,d
#3       c        c

Upvotes: 1

Chirayu Chamoli
Chirayu Chamoli

Reputation: 2076

You can do this:

lapply(lapply(strsplit(as.character(df$fact), split = ','), function(x) x[c(length(x)-1,length(x))]), paste, collapse = ',')

You split the col and then extract the n and n-1 index. Then paste them together.

You can generalise this for by doing:

lapply(strsplit(as.character(df$fact), split = ','), function(x) x[(length(x)-n):length(x)] )

where n is no of backward steps you want to take.

Using tail is even simpler.

lapply(strsplit(as.character(df$fact), split = ','), tail, n=2)

Upvotes: 3

Related Questions