2

I have the following data frame:

Step    1   2   3
1       5   10  6
2       5   11  5
3       5   13  9
4       5   15  10
5       13  18  10
6       15  20  10
7       17  23  10
8       19  25  10
9       21  27  13
10      23  30  7

I would like to retrieve the columns that satisfy one of the following conditions: if step 1 = step 4 or step 4 = step 8. In this case, column 1 and 3 should be retrieved. Column 1 because the value at Step 1 = value at step 4 (i.e., 5), and for column 3, the value at step 4 = value at step 8 (i.e., 10).

I don't know how to do that in R. Can someone help me please?

4
  • @akrun I found it really difficult to write a short title that explains what I exactly want. Commented Feb 22, 2019 at 15:28
  • Can you please describe in the post about what you meant by step1 = step 4 Is that values in column 'step' equal to other column? or is it the duplicate elements? df1[sapply(df1, function(x) max(rle(x)$lengths)>=4),] Commented Feb 22, 2019 at 15:28
  • or may be df1[sapply(df1, function(x) length(unique(x[1:4])) == 1 | length(unique(x[4:8]))),] Commented Feb 22, 2019 at 15:31
  • Can you please add an 'expected output' table so we see what the final results should look like? Commented Feb 22, 2019 at 15:44

1 Answer 1

2

You can get the column indices by the following code:

df[1, -1] == df[4, -1] | df[4, -1] == df[8, -1]

#     X1    X2   X3
# 1 TRUE FALSE TRUE

# data
df <- structure(list(Step = 1:10, X1 = c(5L, 5L, 5L, 5L, 13L, 15L, 
17L, 19L, 21L, 23L), X2 = c(10L, 11L, 13L, 15L, 18L, 20L, 23L, 
25L, 27L, 30L), X3 = c(6L, 5L, 9L, 10L, 10L, 10L, 10L, 10L, 13L, 
7L)), class = "data.frame", row.names = c(NA, -10L))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.