0

In the past, I created a label in a new column like

df$type[df$variable %in% c("sw.1.", "sw.2.", "sw.3.", "sw.4.", "sw.5.")]<-"water"
df$type[df$variable %in% c("st.1.", "st.2.", "st.3.", "st.4.", "st.5.")]<-"temp"

but now I am using dplyr and would like to pipe it in. However, I can't figure out how to do this with mutate. Here is my first shot:

mutate(df, type = ((select(df, starts_with("sw"))) == "temp"))

but it doesn't work.

Some example data:

plot    variable   value
3        sw1        4
4        sw1        5
3        sw1        4
4        sw2        2
5        sw2        3
3        st1        4
4        st2        5
5        st1        5
4        st2        2 
Nazer
  • 3,654
  • 8
  • 33
  • 47
  • 1
    Does this answer your question? [Can dplyr package be used for conditional mutating?](https://stackoverflow.com/questions/24459752/can-dplyr-package-be-used-for-conditional-mutating) – divibisan Jun 16 '20 at 14:20

2 Answers2

2

Try this:

   > mutate(df, type = c("water", "temp") [ 
                               grepl("sw", variable)+2*grepl("st", variable)])

  plot variable value  type
1    3      sw1     4 water
2    4      sw1     5 water
3    3      sw1     4 water
4    4      sw2     2 water
5    5      sw2     3 water
6    3      st1     4  temp
7    4      st2     5  temp
8    5      st1     5  temp
9    4      st2     2  temp

This uses indexing constructed from the sum of two logical vectors. Could be extended to more cases more compactly that nested ifelse statements. If wanted a "neither" or "other" option could make that the first item and add 1 to the logical vectors.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I don't understand much of this, but it works with my data. It's the first time I have seen grepl and I'm not sure what is being done at `+2*`. – Nazer Aug 28 '15 at 19:16
  • I mistaken added my explanation to akrun's answer. `grepl` can be very usefull, more so than grep, since it is always the same length as its input vector. – IRTFM Aug 28 '15 at 19:28
1

We could use ifelse to create the 'type' column

 mutate(df, type= ifelse(variable %in%  paste0('sw', 1:5), 'water', 'temp'))
#  plot variable value  type
#1    3      sw1     4 water
#2    4      sw1     5 water
#3    3      sw1     4 water
#4    4      sw2     2 water
#5    5      sw2     3 water
#6    3      st1     4  temp
#7    4      st2     5  temp
#8    5      st1     5  temp
#9    4      st2     2  temp
IRTFM
  • 258,963
  • 21
  • 364
  • 487
akrun
  • 874,273
  • 37
  • 540
  • 662
  • This doesn't end up working with my real dataset, which ranges from sw1:sw5 and st1:st5, scattered over the dataframe. – Nazer Aug 28 '15 at 19:17
  • 1
    @Nazer You can change to `paste0('sw', 1:5)` I code this based on the example showed. – akrun Aug 28 '15 at 19:18
  • That does help, but the pattern matching works a little better for me right now because my other variables also start with `sw`, but what follows `sw` is not consistent. Sorry I didn't include that in my example data. – Nazer Aug 28 '15 at 19:29
  • 1
    @Nazer It's okay. But, it is better to show an example that mimics the original dataset. – akrun Aug 28 '15 at 19:30