rnorouzian
rnorouzian

Reputation: 7517

conditional subsetting from data.frame in R

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] 

Upvotes: 0

Views: 372

Answers (1)

Shree
Shree

Reputation: 11140

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)

Upvotes: 1

Related Questions