Bex
Bex

Reputation: 201

colsplit in r: separate one column into two

I have a csv file ("sumCounts") loaded in to r which contains a column called "transcript". An example of a row in this column is show below:

TR43890|c0_g1_i1

I want to split this column into two columns called "transcript" and "isoform" along the pipe "|" character.

sumCounts <- colsplit(transcript, "|", c("transcript", "isoform"))

I keep getting the following error: Error in str_split_fixed(string, pattern, n = length(names)) : object 'transcript' not found

Upvotes: 0

Views: 1309

Answers (1)

Forrest R. Stevens
Forrest R. Stevens

Reputation: 3485

Your question doesn't contain quite enough information to know whether this will work, but I'm assuming your data is read in to a data object named sumCounts, with a column named transcript that you want separated into two. If that's the case then Hadley Wickham's tidyr package will do what you want:

install.packages("tidyr")
require(tidyr)

#sumCounts <- read.csv("sumCounts.csv")

## Toy example:
sumCounts <- data.frame(
  "transcript"=c(
    "TR43890|c0_g1_i1", 
    "TR43890|c0_g1_i1", 
    "TR43890|c0_g1_i1"
  )
)

##  Note that the sep= argument requires a regular expression, for which
##    the pipe argument is a special character and must be escaped:
separate(sumCounts, transcript, c("transcript", "isoform"), sep="\\|")

Upvotes: 1

Related Questions