pedro almeida
pedro almeida

Reputation: 123

get all rows that have a certain year

I have this matrix that has the name of a person, the date of birth and their respective sex:

Name     Date          Sex
A       2017-08-01      M
B       2018-06-02      F
C       2019-06-03      F

What I want to do in R is to get the number of people with name A that born in 2017 but I don´t know how.

Here is my code so far, that gives us the people with the name A:

df[format(as.Date(matrix$Date),'%Y')=='2017' & matrix$Name=='A', ]

Upvotes: 0

Views: 969

Answers (2)

Necroticka
Necroticka

Reputation: 285

As Ronak has mentioned, perhaps try to use data frames when you are using heterogeneous data - matrices only allow one data type! You can probably coerce the data to a data frame using as.data.frame().

If you'd like the number of people for each name-year combination, you could try using the following 'tidy' solution:

library(lubridate)
library(dplyr)

sample_data <- data.frame(Name = c("A", "B", "C"), 
                          Date = c("2017-08-01", "2018-06-02", "2019-06-03"), 
                          Sex = c("M", "F", "F"))

sample_data %>% mutate(Year = year(Date)) %>% count(Name, Year)

# A tibble: 3 x 3
  Name   Year     n
  <fct> <dbl> <int>
1 A      2017     1
2 B      2018     1
3 C      2019     1

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389055

If you have a matrix, convert it to dataframe since it is easy to deal with them.

We then need to get the year from Date. Using base R you can do

df[with(df, format(as.Date(Date), "%Y") == "2017" & Name == "A"), ]

# Name        Date Sex
#1    A 2017-08-01   M

Or using packages

library(dplyr)
library(lubridate)

df %>% filter(year(Date) == 2017 & Name == "A")

data

df <- structure(list(Name = structure(1:3, .Label = c("A", "B", "C"
), class = "factor"), Date = structure(1:3, .Label = c("2017-08-01", 
"2018-06-02", "2019-06-03"), class = "factor"), Sex = structure(c(2L, 
1L, 1L), .Label = c("F", "M"), class = "factor")), class = 
"data.frame", row.names = c(NA, 
-3L))

Upvotes: 0

Related Questions