Reputation: 37
The data set I'm working with has 13 columns with the following headers (bold), and 302 rows : id: 001, 002,..., 302), source_code: : AAA, BBB, CCC, date,day, month, year, time,hour, minute, second,latitude, longitude, inscriptions: NA, 1 or 0.
I have a script that creates density maps using this dataset, however, I want to be able to use filters that select the data I want the maps to include and exclude.
example 1: I want to ONLY select the data with id = 1-123 ( and name this selection of data: data_A)
example 2: I want to ONLY select the data with id = 124-168 (and name this: data_B )
example 3: I want to ONLY select the data with id = 1-168 (and name this: data_AB )
example 4: I want to ONLY select the data where id = 169-302 (name this: data_C)
example 5: I want to ONLY select the data where id = 3 (name this: data_3)
I am new using Rstudio and this platform to ask questions, so sorry beforehand if this explanation is vague!
Thank you!!
Upvotes: 2
Views: 676
Reputation: 886968
We can use %in%
to subset the 'id' based on the range of sequence (:
) and create new objects
id1 <- as.numeric(data$id)
data_A <- data[id1 %in% 1:123,]
data_B <- data[id1 %in% 124:168,]
data_AB <- data[id1 %in% 1:168,]
data_C <- data[id1%in% 169:302,]
data_3 <- data[id1 == 3,]
Or if we want to keep the range also as string
data_A <- data[data$id %in% sprintf('%03d', 1:123),]
data_B <- data[data$id %in% sprintf('%03d', 124:168),]
Upvotes: 1
Reputation: 111
Please always include a reproducible minimal example, this makes it a lot easier to help you! Especially using GNU R
this is relatively easy.
From what I understand the filter
-function in dplyr
(or poorman
) can accomplish this for you. I wrote an example where you can filter according to the id
-column.
library(dplyr)
df <-
data.frame(c(1,2,3,4,5),
c("a","b","c","d","e"),
c(NA,0,1,NA,0))
colnames(df) <- c("id","letter","0or1")
df %>%
dplyr::filter(id <= 3)
Upvotes: 1