Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#' Collect Common id
#'
#' Load the Fadn.raw.rds data (Data Table) or Fadn.str.rds data (List),
#' then collection the common id from different years on this data.
#'@param my.r.data A data object(either a data.table or a list).
#'
#'
#' @return A data.table, it includes just one column that named "common_id".
#' @export
#' @author Xinxin Yang
#'
#' @examples
#' collect.common.id(fadn.raw.rds)
#' ## collection the common "id" from the raw rds data
#' ## for 2009-2012 years and country "BEL".
#' ## Return a DT with one column named "common_id".
#'
collect.common.id <- function(my.r.data) {
if (data.class(my.r.data)=="data.table")
{
my.data = my.r.data
}
else{
cat("Tranforming ", data.class(my.r.data), " to data table....\n")
library(tidyverse)
my.data = bind_rows(my.r.data$info)
if ( names(my.data)[1] != "ID"){
setnames(my.data, "id", "ID")
}
}
#print(my.data)
#check how many years in the DT
years = unique(my.data$YEAR)
print(years)
count_year = length(years)
cat(count_year, " year(s) is/are selected.\n")
# create a empty DT
df <- data.table(YEAR = factor(), ID = factor())
# subset the DT
for (year in years){
sel = subset(my.data,YEAR==year,select = ID)
new = data.table(YEAR = year, sel)
df = rbind(df, new)
}
# count the id
count_df = df[, .(count = .N), by = ID]
#print(count_df)
# collect the id, which count = count_year
final = count_df[count_df$count==count_year]
#rename the colname
names(final)[1] = "common_id"
return(final[,1])
}
##################
# check common id
#
# step1:
# read Fadn data
# raw.rds? str.rds?
# FADN raw data: DT
# FADN str data: List id -> ID
#
# step2:
# group by year
#
# step3:
# collect the common id for different years
###################