Extract specific portion of a string and paste to a new column in R
1
0
Entering edit mode
Nikhil • 0
@c0886e17
Last seen 20 months ago
Germany

I have the following dataframe with a string column and I want to extract T,N,M,G,L status (and so on..) for each observation into separate new columns including their respective prefix and suffix. I have tried the grep() and strsplit function but the resulting columns have differing number of rows due to NA values and it doesn't seem to work. I'm not an expert in coding and I'd really appreciate your support for a working script. Thanks in advance.

Code should be placed in three backticks as shown below

df <- c("cT1b;cN1a;cM0;G3", "pT1a;pN0;cM0;G1;L0;V0;Pn0;R0", "cT3;cN0;M0", "rpT1b;rcN0;M0;L0;V0;Pn0;R0", "pT1b;pN0;L0;V0;Pn0;R0", "cT1;cN0; MX", "cT4d;cN1a;M0")

tmp<- trimws(strsplit(df, ";"))
if(length(grep("T", tmp)>0)){ df$G=tmp[grep("G", tmp)]; }
Rscript tidyr dataframe tidyverse offtopic • 1.1k views
ADD COMMENT
0
Entering edit mode

Please direct your question to another website such as stackoverflow.com. Be sure to include:

  • reproducible example
  • the explicit result you wish to obtain (e.g., the resulting data.frame)
  • additional details regarding how data should be split

Best regards, Marcel

ADD REPLY
0
Entering edit mode

This post does not fit the theme of this forum.

ADD REPLY
1
Entering edit mode
@james-w-macdonald-5106
Last seen 2 hours ago
United States

This support site is intended to help people with questions about Bioconductor software rather than general R string manipulation. You should direct your question to either R-help (r-help@r-project.org) or maybe stackoverflow.com or biostars.com.

When you do so, you should try to rephrase your question. I don't know what T,N,M,G,L status is, and you shouldn't assume that anybody else will. You should instead say exactly what you are trying to do. There are lower case p and c in your strings. Do they matter? What should the output for your data look like? You should show an example of the input you have and the hoped for output.

ADD COMMENT

Login before adding your answer.

Traffic: 868 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6