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)]; }
Please direct your question to another website such as stackoverflow.com. Be sure to include:
data.frame
)Best regards, Marcel
This post does not fit the theme of this forum.