can't find or install function as.data.table

Hi

I'm trying to run the code below, and I keep getting 'Error in as.data.table(.) : could not find function "as.data.table"'

I think that as.data.table is part of data.table. I was able to install data.table using install.packages() but that didn't help. If something's not being interpreted, maybe it has to do with magrittr? I thought I installed magrittr also...

I'm not sure what else to try, and appreciate any advice.

There are csv files in the 'discontinuity_mismatch' folder, but they're too big to include here. For example, USG1 0.0.csv looks like this

2020, 2025, 2030, 2035, 2040, 2045, 2050, 2055, 2060
false, false, false, false, false, false, false, false, false
false, false, false, false, false, false, false, false, false
false, false, false, false, false, false, false, false, false
false, false, false, false, false, false, false, false, false
true, true, true, false, false, false, false, false, false
...etc for 10,000 rows

## Clear workspace
rm(list = ls())
gc()

## This function will check if a package is installed, and if not, install it
list.of.packages <- c('tidyverse','magrittr','stringr')
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages, repos = "http://cran.rstudio.com/")
lapply(list.of.packages, library, character.only = TRUE)

##########################
################  PREAMBLE
##########################

## List of gases
hfc_list <- c('23','32','125','134a','143a','152a','227ea','236fa','245fa','4310mee')

##########################
##############  START LOOP
##########################

for (hfc in hfc_list) {
  
page_dir    <- paste0("/home/julia-1.6.3/share/julia/MimiIWG-HFC/data/hfc",hfc,"/page/discontinuity_mismatch") # location of file group
page_files  <- fs::dir_ls(page_dir, regexp = "\\.csv$") # create list of .csv files
page        <- page_files %>% 
                  map_dfr(read_csv, .id = "source") %>% # read in files (map), turn into data frame (df), and row bind (r)
                  as.data.table()

##########################
###################  CLEAN
##########################

page %<>% mutate(source = str_remove(source,paste0("data/hfc",hfc,"/page/discontinuity_mismatch/"))) %>%
          mutate(source = str_remove(source,".csv")) %>%
          separate(source, c("scenario","discount_rate"), " ") %>%
          mutate(scenario = case_when(scenario=="USG1" ~ "IMAGE",
                                      scenario=="USG2" ~ "MERGE Optimistic",
                                      scenario=="USG3" ~ "MESSAGE",
                                      scenario=="USG4" ~ "MiniCAM Base",
                                      scenario=="USG5" ~ "5th Scenario"),
                 discount_rate = paste0(as.numeric(discount_rate)*100,'%')) %>%
          group_by(scenario,discount_rate) %>%
          mutate(trial = seq(n()))

## WIDE TO LONG
years <-  paste(seq(2020,2060,5), sep=", ") # vector of years
page %<>% gather(year,discontinuity,all_of(years)) %>%
          mutate(model = 'PAGE 2009')

##########################
####################  SAVE
##########################

write_csv(page, paste0("home/julia-1.6.3/share/julia/MimiIWG-HFC/data/hfc",hfc,"_page_discontinuity.csv"))

}

## END OF SCRIPT. Have a great day!

Let's start out by cutting and pasting the reprex below in a new session to confirm that data.table is installed

library(data.table)
nn = c(a=0.1, b=0.2, c=0.3, d=0.4)
as.data.table(nn)
#>     nn
#> 1: 0.1
#> 2: 0.2
#> 3: 0.3
#> 4: 0.4

It looks like you do not have the data.table library loaded.

Try

library(data.table) 

at the top of the script. You may even have to install the package data.table.

Yes, thanks for your help. I think that was it. I reopened R and the reprex from technocrat and it worked. I guess that a package needs to be both installed and loaded in order to work-?

1 Like

You only need to install them once but you need to load them on each session you want to use them on.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.