Code Monkey home page Code Monkey logo

Comments (1)

andkov avatar andkov commented on September 26, 2024

Here's reformulation of the same logic using dplyr phraseology

> ds <- ds_valid %>% 
+   dplyr::select(id, age, wave, firstobs, gait) %>% 
+   dplyr::filter( id %in% c("617643","709354","228190"))
> 
> ds %>% glimpse()
Observations: 22
Variables: 5
$ id       <int> 228190, 228190, 617643, 617643, 617643, 617643, 617643, 617643...
$ age      <dbl> 74, 74, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 78, 69, 70...
$ wave     <int> 0, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 0, 1, 2, 3, 4, 5, ...
$ firstobs <dbl> 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
$ gait     <dbl> 0.57, NA, 0.87, 0.76, 0.94, 0.83, 0.80, 0.70, 0.92, 0.75, NA, ...
> ids_gait_70 <- ds %>% 
+   dplyr::filter( age > 67 & age <  73) %>%
+   dplyr::group_by(id) %>% 
+   dplyr::arrange(age) %>% 
+   dplyr::mutate(gait70 = dplyr::first(gait)) %>% 
+   dplyr::distinct(id, gait70)
> ids_gait_70 %>% glimpse()
Observations: 2
Variables: 2
$ id     <int> 617643, 709354
$ gait70 <dbl> 0.94, 1.08
> 
> d2 <- ds %>% 
+   dplyr::left_join(ids_gait_70, by = "id") %>% 
+   # keep only people who have a non-missing gait70
+   dplyr::filter(!is.na(gait70)) %>% 
+   dplyr::filter(age > 68 )
> 
> d2 %>% head(40)
       id age wave firstobs gait gait70
1  617643  68    3        0 0.83   0.94
2  617643  69    4        0 0.80   0.94
3  617643  70    5        0 0.70   0.94
4  617643  71    6        0 0.92   0.94
5  617643  72    7        0 0.75   0.94
6  617643  73    8        0   NA   0.94
7  617643  74    9        0 0.81   0.94
8  617643  75   10        0 0.70   0.94
9  617643  78   13        0   NA   0.94
10 709354  69    0        1 1.08   1.08
11 709354  70    1        0 0.96   1.08
12 709354  71    2        0 0.61   1.08
13 709354  72    3        0 0.44   1.08
14 709354  73    4        0 0.54   1.08
15 709354  75    5        0 0.69   1.08
16 709354  75    6        0 0.59   1.08
17 709354  76    7        0   NA   1.08
> 

from ialsa-2018-amsterdam.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.