Checking the Migration of Variables in Columns with the Same Id on R

Asked 2 years ago, Updated 2 years ago, 39 views

Preprocessing in progress using R3.3.2 and package tidyverse.
Regarding the data below, we have investigated the same id multiple times (chousa has investigated the number of times).
I would like the data from the same survey item (Q1) to the 1st, 2nd, and 4th time since it was changed from 0 to 1.
How should I mute it?
Thank you for your cooperation.

ID Q1 Q6chousa...
1    0   3    1
1    1  23     2 
1    1
2    0  27     3 
2    0  28     4 
2    1  29     5

In this case,

ID change1change2... 
 1     0        0
 1   1        0
 1      0        1
 2      0        0
 2      0        0
 2      1        0

I would like to make it

add
If Q1 says 0,0,1,1,1,1,10 for the same ID,

change1:0,0,1,0,0,0,0
change2—0,0,0,1,0,0,0
change3—0,0,0,0,0,1,0,0
change4—0,0,0,0,0,0,1,0
change5—0,0,0,0,0,0,0,0

I would like to make it

mute(change1=ifelse(Q1==1&Q1-lag(Q1)==1,1,0),
change2 = ifelse(Q1 == 1 & lag(change1) == 1,1,0),
change3=ifelse(Q1==1&lag(change2)==1,1,0),
change4=ifelse(Q1==1&lag(change3)==1,10))

I think I can go there by

 id change 1 change 2 change 3
 5      2       1       0       0
 5      3       0       1       0
 5      4       0       0       1

What I want you to do is

 id change 1 change 2 change 3
 5      2       0       0       1
 5      3       0       1       0
 5      4       1       0       0

It will be the opposite of .When I did group_by, it seems that the ascending and descending order of the ID is reversed. What should I do...

→ Not only group_by(ID), but also array(ID, chousa) resolved it.

r

2022-09-30 21:21

1 Answers

I don't understand the question properly, but if you do the following, the desired form will be printed.
(Determination of change1, change2 is hard to understand)

I think it's a lie to process each ID.

library(tidyverse)
data_frame(
     ID = c(rep(1,3), rep(2,3)),
     Q1 = c(0,1,1,0,0,1),
     chosa = c (1, 2, NA, 3, 4, 5)
%>% 
  # Aggregate by ID
  group_by(ID)%>% 
    # change1 —Subtract the value of Q1 in the next line from the Q1 in the target line and the Q1 in the target line.
    #   At this time, the first line of the ID is NA and 0 for continuous values.
    # change2 —Takes the difference between the value of change1 and the value of Q1 so that if Q1 follows the same value, it becomes 1.
  mute(change1 = Q1-lag(Q1),
         change2 = Q1-change1)%>% 
  ungroup()%>% 
  # treat NA as zero
  mute_at(num_range("change", 1:2), funs(ifelse(test=is.na(.), yes=0, no=.))%>% 
  select(ID, num_range("change", 1:2))

I don't understand the logic, so if the number of data increases, it might be ruined.For your information.


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.