Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress separator in paste when values are missing

Tags:

r

ggplot2

paste

This is my data structure that I read into a dataframe.

treatment  egf       mean      se
10 uM PP2            -697.25   14124.349
10 uM PP2  1 nM EGF  14715.50  8862.012
DMSO                 58589.25  7204.824
DMSO       1 nM EGF  87852.00  12149.159

The combination of the treatment and egf columns represent the unique id for each column. I would like to create a column that combines these so that I can have one column that uniquely represents each row. However, because of the missing values in the EGF column, when I use paste, it does this annoying thing:

>paste(rawp$treatment, rawp$egf, sep=" + ")
[1] "10 uM PP2 + "         "10 uM PP2 + 1 nM EGF" "DMSO + "             
[4] "DMSO + 1 nM EGF"

where it will still place the separator there when the value is missing. I would like for it to read:

[1] "10 uM PP2"         "10 uM PP2 + 1 nM EGF" "DMSO"             
[4] "DMSO + 1 nM EGF"

How can I do this?

The whole reason I want to do this is because I want to plot the data with ggplot and it seems it requires only 1 unique column when specifying the x axis.

ggplot(data=rawp, aes(x=treatment, y=mean)) + geom_bar(stat="identity")

So if you also know of an alternative way to use combined columns to specify the category on the x axis, that would be helpful.

like image 885
user1968672 Avatar asked Jan 11 '13 02:01

user1968672


People also ask

What does collapse do in paste?

The paste() function with collapse argument When you pass a paste argument to a vector, the separator parameter will not work. Hence here comes the collapse parameter, which is highly useful when you are dealing with the vectors. It represents the symbol or values which separate the elements in the vector.

What is paste() in R?

paste() method in R programming is used to concatenate the two string values by separating with delimiters. Syntax: paste(string1, string2, sep=) Return: Returns the concatenate string.


1 Answers

It would have been nice to use sep = ifelse(egf == "", "", " + ") but the sep argument to paste cannot be a vector. The solution: pass it as a regular argument!

with(rawp, paste0(treatment, ifelse(egf == "", "", " + "), egf))
like image 132
flodel Avatar answered Sep 29 '22 01:09

flodel