4111

selection of observations by combining criteria in R

Question:

This topic has probably been brought up and it is a quite simpe solution , i guess. However i couldnt make it up to now. Lets say i have a data.frame (called "data") which contains 10 individuals (id) on which i collected observations at 3 time points (T)

> data <- data.frame(id = rep(c(1:10), 3), T = gl(3, 10), X = sample(1:30), Y = sample(c("yes", "no"), 30, replace = TRUE), Z = sample(1:40, 30), Z2 = rnorm(30, mean = 5, sd = 0.5)) > head(data) id T X Y Z Z2 1 1 1 10 yes 15 5.993605 2 2 1 18 no 22 6.096566 3 3 1 5 no 24 5.101393 4 4 1 15 yes 18 4.944108 5 5 1 23 no 34 4.634176 6 6 1 13 no 27 5.576015

I would like to create a subset of this data.frame (an new data.frame called data2) by selecting only individuals that have "yes" (variable Y) for each of the three time points (variable T), that means Y="yes" for T=1 and T=2 and T=3.

I know that combining conditions can be achieved by using the "&" sign, and this can be used to relate conditions for the 3 time points. However, my problem is to write each condition for each time point : how to tell R that i want subjects for which Y="yes" at T="1" for example ?

Thank you very much in advance to all. Have a great day,

Denis

Answer1:

You can do:

keep.ids <- tapply(data$Y, data$id, FUN = function(x)all(x == "yes")) subset(data, keep.ids[factor(id)])

Or use the plyr package:

library(plyr) ddply(data, "id", function(x) if(all(x$Y == "yes")) x else NULL)

Recommend

  • Parse Phone Number into component parts
  • Java: successful build but the output is “user_package.Point@68e26d2e”
  • Cant find the header when I linka .lib in visual studio 2012
  • Compiling GLUI library, VS2010 gives me a weird namespace error
  • Crash in program using OpenMP, x64 only
  • Document.body doesn't exist. Modernizr hyphens test needs it [closed]
  • LibGDX: Android SpriteBatch not drawing
  • Trying to Graph a Simple Square in pyOpenGL
  • Excel changes conditional formatting formula
  • GLSL texture2D() always returns (0, 0, 0, 1)
  • GLUT compile errors with Code::Blocks
  • Multiple values for an AND/OR statement
  • ggplot2: Plotting regression lines with different intercepts but with same slope
  • R ggvis interactive slider for calculating y values (e.g. for background correction)
  • Recreate Apple Watch fitness tracker ‘progress’ bar - gradient on CAShapeLayer stroke
  • R Impute NA's by Linear Increase Depending on Time Interval
  • How to model a mixture of finite components from different parametric families with JAGS?
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • Is mp4 stream able with ffserver?
  • Geom_jitter colour based on values
  • JSON encode and decode on PHP
  • SAXReader not re-ecape characters
  • Reading a file into a multidimensional array
  • Android application: how to use the camera and grab the image bytes?
  • Problems compiling files using JOGL
  • Checking free space on FTP server
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Knitr HTML Loop - Some HTML output, some R output
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal