6

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.

1

5 Answers 5

5

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))
5

You can strip out the "+" after pasting:

sub(" \\+ $", "", paste(rawp$treatment, rawp$egf, sep=" + "))
1

Since it's harder to duplicate your example, I made my own:

d <- data.frame(a=c('a', 'b', ''), b=c('c', 'd', 'e'))

What you have:

paste(d$a, d$b, sep=' + ')
## [1] "a + c" "b + d" " + e" 

What you want:

unlist(apply(d[1:2], 1, function(x) paste(x[x!=''], collapse=' + ')))
## [1] "a + c" "b + d" "e"    

The solution is similar if you have NA instead of '' as the blank value. Use !is.na(x) instead of x!='' in that case.

This extends to the case where you have more than two columns, and where you might have missing values in any column:

d1 <- data.frame(a=c('a', 'b', ''), b=c('c', 'd', 'e'), c=c('', 'f', ''))
unlist(apply(d1, 1, function(x) paste(x[x!=''], collapse=' + ')))
## [1] "a + c"     "b + d + f" "e"     
1

This is a great example of when ifelse would work nicely:

ifelse(rawp$egf=="", paste(rawp$treatment), 
                     paste(rawp$treatment, rawp$egf, sep=" + "))
0

You can use apply to take each line of the data.frame and strip out all elements of 0 length, then use the collapse argument to bring back to a single string:

apply(rawp[1:2],1,function(x) paste(x[nchar(x)>0],collapse=" + "))
[1] "10 uM PP2"            "10 uM PP2 + 1 nM EGF" "DMSO"                
[4] "DMSO + 1 nM EGF" 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.