Jessi
Jessi

Reputation: 25

Subsetting multiple variables in one column in r

I only have basic knowledge of R and i hope you can help me with my problem and its not a too stupid question for you ;-)

I have a dataset called "rope". It looks like the following :

head(rope)

    X...Sound Time.real. Time.in.Video. Observations
1 5_min_blank      10:18      03:59 (2)            2
2 5_min_blank                                     NA
3     Fisch1       10:23          08:59            6
4     Fisch1                                      NA
5     Fisch1                                      NA
6     Fisch1                                      NA
  Observation.total.time Time.of.the.shark.in.the.video
1                     60                             23
2                                                    37
3                    157                             17
4                                                    46
5                                                    37
6                                                    28
  Time.of.the.shark.entering.the.video
1                                04:03
2                                04:20
3                                08:49
4                                09:06
5                                09:23
6                                10:21
  Time.of.the.shark.leaving.the.video
1                               04:26
2                               04:57
3                               09:05
4                               09:52
5                               10:00
6                               10:49
  times.the.shark.turns.to.the.speaker directional.change
1                                    1                  5
2                                    2                 11
3                                    1                  1
4                                    4                  6
5                                    3                  6
6                                    2                  7
  flap.of.the.fins..fotf. flap.of.the.fins..second corrected.fotf.s
1                      14              0,608695652        0.7777778
2                      14              0,378378378        0.5600000
3                       0                                        NA
4                      30              0,652173913        0.6818182
5                       0                        0               NA
6                      15              0,535714286        0.6521739
  Notes complete.cyrcles swims.below.b..above.a..speaker
1                      1                              NA
2                                                     NA
3                                                     NA
4                      2                              NA
5                                                     NA
6                                                     NA
  Swimming.patterns     date  X
1                 3 21.07.17 NA
2                 9 21.07.17 NA
3                NA 21.07.17 NA
4                 9 21.07.17 NA
5                 4 21.07.17 NA
6                 4 21.07.17 NA

Now i have different sounds. The first sound is the "Fish1" but i also have "Fish2" and "Diving" for example. Furthermore are between the sounds the corresponding pauses they are called "Fish1_pause", "Fish2_pause" or "Diving_pause" etc. Now i would like to subset my data into the sound data points and the "pause" data points. I tried:

sound<-subset(rope, rope$X...Sound=="Fish1"& rope$X...Sound=="Fish2")

but i got no datapoint at all... if i only type :

sound<-subset(rope, rope$X...Sound=="Fish1")

I receive all datapoints were i have the Fish1 sound. My question now is how can i get all sound points? Because with the "&" it didn't work... i hope you understand my problem and you can help me.

Thank you very much and all the best

Jessi

Upvotes: 0

Views: 1938

Answers (1)

John Coleman
John Coleman

Reputation: 51998

sound<-subset(rope, rope$X...Sound=="Fish1"& rope$X...Sound=="Fish2")

should be replaced by either

sound<-subset(rope, rope$X...Sound == "Fish1" | rope$X...Sound == "Fish2")

or

sound<-subset(rope, rope$X...Sound %in% c("Fish1","Fish2"))

As it is, you are asking for observations where X...Sound is simultaneously "Fish1" and "Fish2" -- which is impossible.

Upvotes: 1

Related Questions