r - Finding 1 occurrence of a value within array of variables in a dataframe -


I am transitioning from SAS to R and am "feeling" some difficulties.

The following dataset:

  key var1 var2 var3 var4 res1 res2 res3 res4 1 1 2 3 4 100 121 34565 4345 2 2 1 4 3 200 56 345 345 3 4 3 2 1 100 34 3 6 4 1 4 1 2 100 3 2 43 5 1 4 4 2 400 6 34 3 structure (list (key = 1: 5, var1 = c (1l, 2l, 4l, 1l, 1L), var2 = c (2L, 1L, 3L, 4L, 4L), VAR3 = C (3L, 4L, 2L, 1L, 4L), 4W (4L) , 3L, 1L, 2L, 2L), Race 1 = C (100 L, 200 L, 100 L, 100 L, 400 L), Ridge 2 = C (121 L, 56 L, 34 L, 3 L, 6 L), Riz 3 = C (34565 L, 345 L, 3 L, 2 L, 34 L), Race 4 = C (4345 L, 345 L, 6 L, 43 L, 3 L)). = C ("key", "var1", "var2", "var3", "var4", "res1", "res2", "res3", "res4"), class = "data.fr  var1  -  var4  for example     The time (var1 - J Un, var2-jul,  etc.) goes to the same array res1-res4 but this is an amount I'm trying to do: 

  1. To find the first appearance (working in line) for the key of 4 credits. Therefore for example a new variable example should be FirstTime4Credits & lt; The index array of (var1-var4) should be the result of the first row 4 (the first 4 lines 4 var4 = 4), the second 3, the third row = 1, etc.
  2. Then I need to see what point goes back to 1 point (first row-NA, second line = 4, 3rd row 3, fourth row = NA) after reaching 4 And makes a new variable while representing it.
  3. After all, I need to take the amount from res1-res4 based on the client's 4 credits and moments based on 2 different variables after 1 credit.

The first thing I managed to do:

  varArray & lt; -c ("var1", "var2", "var3", "var4") d $ first4 & lt; - Apply (D [varArray], 1, Match, X = 4)  

Where "D" is the imported data frame mentioned above.

The second task is a show for me- I am not sure how to begin to see the elements in the list based on the value of the variable.

  • 1 task

      varNames & lt ; - grep ("var", colnames (d)) index & lt; - Max.col (d [, varNames], "first") Indx # [1] 4 3 1 2 2  
  • Second task

  • indx1 & lt; - max.col (df [, varNames] == 1, "last") indx2 & lt; - Ifelse (indx1 & gt; Indx, Indx1, NA) indx2 # [1] NA4 4 NA
  • 3 tasks

      resNames & lt; - grep ("res", colnames (d)) d1 & lt; # [1] [43] [43] 4345 345 100 3 6 D1 [cbind (seq_along (indx2), Indx 2)] # [1] NA 6 2 NA  / Code>    

    data
      d & lt; - structure (list (key = 1: 5, var1 = c (1 L, 2L, 4L, 1L, 1L), var2 = c (2L, 1L, 3L, 4L, 4L), var3 = C (3L, 4L, 2L, 1L, 4L), var4 = c (4L, 3L, 1L, 2L, 2L), res1 = c (100L, 200L, 100L, 100L, 400L) Res2 = c (121L, 56L, 34L, 3L, 6L), ridge 3 = C (34565L, 345L, 3L, 2L, 34L), res4 = c (4345L, 345L, 6L, 43L, 3L), first 4 = C (4L, 3L, 1 L, 2L, 2L)). Name = C ("key", "var1", "var2", "var3", "var4", "res1", "res2", "res3", "res4" "First 4"), row.Name = C (NA, 5L), class = "data.frame")  


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -