0

I have a data.frame HERE. I was wondering how I can subset variables named as dot.names (see below) for rows in the data.frame for which variable control is FALSE AND alphabetically order the answer based on D$study.name in BASE R?

Here is the code I used without success:

D <- read.csv("https://raw.githubusercontent.com/izeh/m/master/k.csv") # data.frame

dot.names <- c("ESL", "prof" ,"scope", "type")             

D[dot.names & !control] 

1 Answer 1

1

If D$control is a logical column -

res1 <- D[order(D$study.name), ]

res2 <- res1[!res1$control, dot.names]

If D$control is character column -

D[D$control == "FALSE", dot.names]

A on-liner using subset -

subset(D[order(D$study.name), ], !control, select = dot.names)

With dplyr -

D %>% 
  filter(!control) %>% 
  arrange(study.name) %>% 
  select(dot.names)
Sign up to request clarification or add additional context in comments.

2 Comments

@rnorouzian They should be. Try- dot.names <- c("mpg", "hp"); res1 <- mtcars[order(mtcars$wt), ]; res2 <- res1[res1$cyl == 4, dot.names]; res3 <- subset(mtcars[order(mtcars$wt), ], cyl == 4, select = dot.names); identical(res2, res3); all.equal(res2, res3).Also try all.equal(). Identical is very strict I think. Let me know.
I think your solutions are not identical nrow for your subset solution gives 33 rows, BUT nrow for your res2 solution gives 40 rows !!!

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.