GitLab at IIASA

common_id.R 1.77 KiB
Newer Older
Xinxin Yang's avatar
Xinxin Yang committed
#' 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
###################