5

Using a simple string like this:

a <- "l_Gf43qCW2r&auty=in_out"

I can keep only the first part using this:

b <- strsplit(a, "&auty=")[[1]][1]

I would like to make the same for a whole column from a dataframe. I tried this:

n = 1
for (i in 1:nrow(df)) {
      c <- strsplit(df$col1[n], "&auty=")[[1]][1]
      n = n + 1
}

but I receive this error:

Error in strsplit(df$col1[n], "&auty=") : non-character argument

Is there any different way for dataframes to make it?

Teres
  • 73
  • 1
  • 8
  • It would make sense to post the result of `dput(df)`, so we can see what your `df` looks like. Side note: you can remove the `n = 1` before the loop and `n = n + 1` inside the loop. Replace the `strsplit(df$col1[n], "&auty=")[[1]][1]` with `strsplit(df$col1[i], "&auty=")[[1]][1]`. – KenHBS Oct 18 '16 at 09:44
  • or make a function `> splitr <- function(x){ strsplit(x, "&auty=")[[1]][1] } ` – Pankaj Kaundal Oct 18 '16 at 09:45
  • Paste the result of `dput(df$col1)` if your problem persists. – 989 Oct 18 '16 at 09:54

3 Answers3

5

Try this:

unlist(lapply(strsplit(as.character(df$col1), "&auty="), '[[', 1))

Applying strsplit on the whole column col1 of data frame df will give you a list in which every element contains the first and second part of the split. By doing so, you extract the first part of each list's element and unlist will give you the result in the form of vector.

as.character(df$col1) will presumably fix your error.

989
  • 12,579
  • 5
  • 31
  • 53
0

Maybe this:

 sapply(df$col1,function(x) gsub("&auty=.*","",x))
Haboryme
  • 4,611
  • 2
  • 18
  • 21
0

One option is using sub

sub("&auty.*", "", a)
#[1] "l_Gf43qCW2r"
akrun
  • 874,273
  • 37
  • 540
  • 662