1

Basically, I have data from a between subjects study design that looks like this:

>    head(have, 3)
              A    B    C
        1          b 
        2     a  
        3               c

Here, A, B, C are various conditions of the study, so each subject (indicated by each row), only has a value for one of the conditions. I would like to combine these so that it looks like this:

>    head(want, 3)
             all
        1     b 
        2     a  
        3     c

How can I combine the columns so that they "overlap" like this?

So far, I have tried using some of dplyr's join functions, but they haven't worked out for me. I appreciate any guidance in combining my columns in this way.

rhonda
  • 13
  • 3

1 Answers1

2

We can use pmax

want <- data.frame(all= do.call(pmax, have))

Or using dplyr

transmute(have, all= pmax(A, B, C))
#    all
#1   b
#2   a
#3   c

data

have <- structure(list(A = c("", "a", ""), B = c("b", "", ""), 
 C = c("", 
"", "c")), .Names = c("A", "B", "C"), class = "data.frame", 
row.names = c("1", "2", "3"))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you for your response, and I totally appreciate the idea here. However, it does not seem to be working - it's returning a df with only one column, which contains only my `a` values, so basically it just returned my `A` column from before. Do you know why that might be? – rhonda Nov 07 '15 at 20:20
  • @rhonda In the input dataset you had three columns and in the expected output you showed one column. That is what the function returns. If you need all the columns from the original dataset, `mutate(df1, all=pmax(A, B, C))` – akrun Nov 08 '15 at 05:00
  • I need one column, but I need one column with all my values (a, b, c) and not just the exact same first column I started with, which is what this code is giving me. So yes, it returns one column and I want one column, but the content of the column is unchanged. Any further insight? – rhonda Nov 08 '15 at 18:22
  • @rhonda From the input example and the output you showed, my solution is not showing any difference. If that is not the output, you should update your post. – akrun Nov 08 '15 at 18:25
  • Thanks for your help. My question is perfect and apparently it's working for you, but it's just not working for me. So I'll keep playing with it. – rhonda Nov 08 '15 at 18:44
  • @rhonda Do you have any NA values in the dataset?. Just to make it reproducible, consider to update with `dput` output. ie. `dput(droplevels(head(have)))` – akrun Nov 08 '15 at 18:45
  • No NAs but I did fix it :) my values were being stored as factors - needed to be characters. Thank you again for your patience and persistence with my problem. – rhonda Nov 08 '15 at 18:54