Remove first and last character

How do I remove the first and last character in example below?

# Given DF
df <- data.frame(
  v1 = c("<Lisbon>", "<London[12]>", "(London/Paris)", "[420-Tokyo]")
)
df
#>               v1
#> 1       <Lisbon>
#> 2   <London[12]>
#> 3 (London/Paris)
#> 4    [420-Tokyo]

# Desired DF
ddf <- data.frame(
  v1 = c("Lisbon", "London[12]", "London/Paris", "420-Tokyo")
)
ddf
#>             v1
#> 1       Lisbon
#> 2   London[12]
#> 3 London/Paris
#> 4    420-Tokyo

Created on 2020-09-27 by the reprex package (v0.3.0)

Hello,

For some reason I couldn't just run it as a normal df through the function but it produces the desired output. See below and let me know if this is an adequate solution.

library(dplyr)

df <- data.frame(
  v1 = c("<Lisbon>", "<London[12]>", "(London/Paris)", "[420-Tokyo]")
)


df2 <- gsub('^.|.$', '', unlist(df)) %>% as.data.frame()

df2
#>                .
#> v11       Lisbon
#> v12   London[12]
#> v13 London/Paris
#> v14    420-Tokyo

Created on 2020-09-27 by the reprex package (v0.3.0)

@GreyMerchant Thanks for the feedback. It works to some extant, but not completely. See the example below.

library(dplyr)

# Modified DF
df <- data.frame(
  v1 = c("<Lisbon>", "<London[12]>", "(London/Paris)", "[420-Tokyo]"),
  v2 = c(1:4) # ***added a new column***
)
df
#>               v1 v2
#> 1       <Lisbon>  1
#> 2   <London[12]>  2
#> 3 (London/Paris)  3
#> 4    [420-Tokyo]  4

# Solution provided by GreyMerchant 
df2 <- gsub('^.|.$', '', unlist(df)) %>% as.data.frame()
df2
#>                .
#> v11       Lisbon
#> v12   London[12]
#> v13 London/Paris
#> v14    420-Tokyo
#> v21             
#> v22             
#> v23             
#> v24

Created on 2020-09-27 by the reprex package (v0.3.0)

Hello @budugulo,

It won't work on numbers in a single column as the first and last character is the same which will essentially remove it and give you nothing in its place. The solution I provided will work for any of the ones you listed in the original post and even if they are non matching (say [ with } as an example).

You can then just do it like the below? If you want it different then you'll have to specify your problem better.

library(dplyr)
#> Warning: package 'dplyr' was built under R version 3.6.3
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- data.frame(
  v1 = c("<Lisbon>", "<London[12]>", "(London/Paris)", "[420-Tokyo]"),
  v2 = c(1:4) # ***added a new column***
)

df2 <- df %>% mutate(v1 = gsub('^.|.$', '', unlist(v1)) %>% as.data.frame())

df2
#>              . v2
#> 1       Lisbon  1
#> 2   London[12]  2
#> 3 London/Paris  3
#> 4    420-Tokyo  4
```

<sup>Created on 2020-09-27 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>

It works now. Thanks!

1 Like

Glad to hear it is working now how you wanted it :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.