Problem with Function not reading data.

I am having a problem converting a working string of code into a function.

It looks like I am messing up in specifying the function variable in the function code. Presumably I am doing something stupid bet I can't see it.

Sample data is at the bottom of the message. Note I am using {data.table}

Thanks

suppressMessages(library(data.table));
suppressMessages(library(tidyverse))
setDF(DT1) # Convert data.frame to data.table

## Working Code
cty <-  DT1[country_code == "ABW"]
nation <- paste(as.character(cty[1, 1]), "Birthrate")

cty_names <- names(cty) 
cty_names <- cty_names[3:length(cty_names)] 
cty_names <-  sub(".", "", cty_names) |> as.numeric()

cty_br <- unlist(cty[1,])
cty_br <- cty_br[3:length(cty_br)] |> as.numeric()

target <- data.table(cty_names, cty_br) 

ggplot(target, aes(cty_names, cty_br)) +
  geom_point(colour = "red") + geom_line(group = 1, colour ="blue")  +
  labs(title = nation, x = "Years", y = "Birthrate")


## Not-working function
myplot <- function(my_country){
  
  cty <-  DT[country_code == "my_country"]
  nation <- paste(as.character(cty[1, 1]), "Birthrate")
  
  cty_names <- names(cty) 
  cty_names <- cty_names[3:length(cty_names)] 
  cty_names <-  sub(".", "", cty_names) |> as.numeric()
  
  cty_br <- unlist(cty[1,])
  cty_br <- cty_br[3:length(cty_br)] |> as.numeric()
  
  target <- data.table(cty_names, cty_br) 
  
  ggplot(target, aes(cty_names, cty_br)) +
    geom_point(colour = "red") + geom_line(group = 1, colour ="blue")  +
    labs(title = nation, x = "Years", y = "Birthrate")
  
}

myplot(ABW)

Sample Data

DT1 <- structure(list(country_name = c("Aruba", "Africa Eastern and Southern", 
                                       
                                       "Afghanistan", "Africa Western and Central", "Angola", "Albania", 
                                       
                                       "Andorra", "Arab World", "United Arab Emirates", "Argentina", 
                                       
                                       "Armenia", "American Samoa", "Antigua and Barbuda", "Australia", 
                                       
                                       "Austria", "Azerbaijan", "Burundi", "Belgium", "Benin", "Burkina Faso", 
                                       
                                       "Bangladesh", "Bulgaria"), country_code = c("ABW", "AFE", "AFG", 
                                                                                   
                                                                                   "AFW", "AGO", "ALB", "AND", "ARB", "ARE", "ARG", "ARM", "ASM", 
                                                                                   
                                                                                   "ATG", "AUS", "AUT", "AZE", "BDI", "BEL", "BEN", "BFA", "BGD", 
                                                                                   
                                                                                   "BGR"), y1960 = c(4.82, 6.72322577683399, 7.282, 6.45906325929944, 
                                                                                                     
                                                                                                     6.708, 6.455, NA, 6.92532650587424, 6.718, 3.075, 4.786, NA, 
                                                                                                     
                                                                                                     4.602, 3.453, 2.69, 5.878, 7.003, 2.54, 6.282, 6.248, 6.784, 
                                                                                                     
                                                                                                     2.31), y1961 = c(4.655, 6.74212391533005, 7.284, 6.47209862023661, 
                                                                                                                      
                                                                                                                      6.79, 6.353, NA, 6.97349319481517, 6.678, 3.069, 4.67, NA, 4.559, 
                                                                                                                      
                                                                                                                      3.54, 2.78, 5.937, 7.023, 2.63, 6.402, 6.286, 6.831, 2.29), y1962 = c(4.471, 
                                                                                                                                                                                            
                                                                                                                                                                                            6.76234266665616, 7.292, 6.49239470136811, 6.872, 6.207, NA, 
                                                                                                                                                                                            
                                                                                                                                                                                            7.068162514806, 6.659, 3.106, 4.521, NA, 4.547, 3.442, 2.8, 5.961, 
                                                                                                                                                                                            
                                                                                                                                                                                            7.038, 2.59, 6.457, 6.322, 6.865, 2.24), y1963 = c(4.271, 6.7781328342706, 
                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                               7.302, 6.50652816532602, 6.954, 6.047, NA, 7.06315409593758, 
                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                               6.619, 3.101, 4.345, NA, 4.536, 3.332, 2.82, 5.945, 7.067, 2.68, 
                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                               6.51, 6.356, 6.841, 2.21), y1964 = c(4.059, 6.78782143731601, 
                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                    7.304, 6.52552945628155, 7.036, 5.849, NA, 7.05309784978593, 
                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                    6.573, 3.08, 4.15, NA, 4.484, 3.146, 2.79, 5.888, 7.09, 2.71, 
                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                    6.565, 6.419, 6.837, 2.19)), row.names = c(NA, -22L), class = "data.frame")


I got it to produce a plot for Aruba (no idea if the plot is correct) by making the following changes to your code.

  • Line 6: cty <- DT1["country_code" == "ABW"] (quotes around country_code)
  • Line 26: cty <- DT1[DT1$country_code == my_country,] (change DT to DT1, change country_code to DT1$country_code, add a comma before the closing bracket to get all the columns)
  • Line 44: Add quotes around ABW

At least some of those (the addition of quotes) may be a matter of the data.table package not being part of the tidyverse.

Thanks. I am still have a problem but I managed to mess up some code to begin with.

I wrote

setDF(DT1

when t should have been

setDT(DT1)

"Pound head on desk!!
I was working with the original data.table. I converted to a data.frame because {data.table} can give weird (and usually meaningless) errors when one is reading it from dput() output.

My apologies about the "DT", it was the original data.table. I missed the edit but the example ran for me as I had "DT` and "DT1" loaded. Carelessness on my part, I should have reloaded the project.

I don't need the " " around

 DT1[country_code == "ABW"]

in the working example. It was probable due to my mistake with setDT that caused the problem.

I still am getting nowhere with the function.

There is what I am getting:

> myplot(ABW)
Warning messages:
1: Removed 5 rows containing missing values or values outside the scale range (`geom_point()`). 
2: Removed 5 rows containing missing values or values outside the scale range (`geom_line()`). 

> myplot("ABW")
Warning messages:
1: Removed 5 rows containing missing values or values outside the scale range (`geom_point()`). 
2: Removed 5 rows containing missing values or values outside the scale range (`geom_line()`). 

New, hopefully cleaned up code below.

Thanks again

suppressMessages(library(data.table));
suppressMessages(library(tidyverse))
setDT(DT1)
str(DT1)

myplot <- function(my_country){

  cty <-  DT1[country_code == "my_country"]
  nation <- paste(as.character(cty[1, 1]), "Birthrate")
  
  cty_names <- names(cty) 
  cty_names <- cty_names[3:length(cty_names)] 
  cty_names <-  sub(".", "", cty_names) |> as.numeric()
  
  cty_br <- unlist(cty[1,])
  cty_br <- cty_br[3:length(cty_br)] |> as.numeric()
  
  target <- data.table(cty_names, cty_br) 
  
  ggplot(target, aes(cty_names, cty_br)) +
    geom_point(colour = "red") + geom_line(group = 1, colour ="blue")  +
    labs(title = nation, x = "Years", y = "Birthrate")
  
}

myplot("ABW")

Sample Data -------------------------------------------------------------

DT1 <- structure(list(country_name = c("Aruba", "Africa Eastern and Southern",
"Afghanistan", "Africa Western and Central", "Angola", "Albania",
"Andorra", "Arab World", "United Arab Emirates", "Argentina",
"Armenia", "American Samoa", "Antigua and Barbuda", "Australia",
"Austria", "Azerbaijan", "Burundi", "Belgium", "Benin", "Burkina Faso",
"Bangladesh", "Bulgaria"), country_code = c("ABW", "AFE", "AFG",
"AFW", "AGO", "ALB", "AND", "ARB", "ARE", "ARG", "ARM", "ASM", "ATG", "AUS",
"AUT", "AZE", "BDI", "BEL", "BEN", "BFA", "BGD",
"BGR"), y1960 = c(4.82, 6.72322577683399, 7.282, 6.45906325929944,
6.708, 6.455, NA, 6.92532650587424, 6.718, 3.075, 4.786, NA,
4.602, 3.453, 2.69, 5.878, 7.003, 2.54, 6.282, 6.248, 6.784,
2.31), y1961 = c(4.655, 6.74212391533005, 7.284, 6.47209862023661,
6.79, 6.353, NA, 6.97349319481517, 6.678, 3.069, 4.67, NA, 4.559,
3.54, 2.78, 5.937, 7.023, 2.63, 6.402, 6.286, 6.831, 2.29), y1962 = c(4.471,
6.76234266665616, 7.292, 6.49239470136811, 6.872, 6.207, NA,
7.068162514806, 6.659, 3.106, 4.521, NA, 4.547, 3.442, 2.8, 5.961,
7.038, 2.59, 6.457, 6.322, 6.865, 2.24), y1963 = c(4.271, 6.7781328342706,
7.302, 6.50652816532602, 6.954, 6.047, NA, 7.06315409593758,
6.619, 3.101, 4.345, NA, 4.536, 3.332, 2.82, 5.945, 7.067, 2.68,
6.51, 6.356, 6.841, 2.21), y1964 = c(4.059, 6.78782143731601,
7.304, 6.52552945628155, 7.036, 5.849, NA, 7.05309784978593,
6.573, 3.08, 4.15, NA, 4.484, 3.146, 2.79, 5.888, 7.09, 2.71,
6.565, 6.419, 6.837, 2.19)), row.names = c(NA, -22L), class = "data.frame")

Forgot. Here is an example of what I am after. The entire series from 1960 to 2022 is not quite that alarming….

I removed the quotation marks around my_countryin the assignment of cty in the function and I added quotes around ABW when calling myplot.

DT1 <- structure(list(country_name = c("Aruba", "Africa Eastern and Southern",
                                       "Afghanistan", "Africa Western and Central", "Angola", "Albania",
                                       "Andorra", "Arab World", "United Arab Emirates", "Argentina",
                                       "Armenia", "American Samoa", "Antigua and Barbuda", "Australia",
                                       "Austria", "Azerbaijan", "Burundi", "Belgium", "Benin", "Burkina Faso",
                                       "Bangladesh", "Bulgaria"), 
                      country_code = c("ABW", "AFE", "AFG",
                                       "AFW", "AGO", "ALB", "AND", "ARB", "ARE", "ARG", "ARM", "ASM", "ATG", "AUS",
                                       "AUT", "AZE", "BDI", "BEL", "BEN", "BFA", "BGD",
                                       "BGR"), 
                      y1960 = c(4.82, 6.72322577683399, 7.282, 6.45906325929944,
                                6.708, 6.455, NA, 6.92532650587424, 6.718, 3.075, 4.786, NA,
                                4.602, 3.453, 2.69, 5.878, 7.003, 2.54, 6.282, 6.248, 6.784,
                                2.31), 
                      y1961 = c(4.655, 6.74212391533005, 7.284, 6.47209862023661,
                                6.79, 6.353, NA, 6.97349319481517, 6.678, 3.069, 4.67, NA, 4.559,
                                3.54, 2.78, 5.937, 7.023, 2.63, 6.402, 6.286, 6.831, 2.29), 
                      y1962 = c(4.471,
                                6.76234266665616, 7.292, 6.49239470136811, 6.872, 6.207, NA,
                                7.068162514806, 6.659, 3.106, 4.521, NA, 4.547, 3.442, 2.8, 5.961,
                                7.038, 2.59, 6.457, 6.322, 6.865, 2.24), 
                      y1963 = c(4.271, 6.7781328342706,
                                7.302, 6.50652816532602, 6.954, 6.047, NA, 7.06315409593758,
                                6.619, 3.101, 4.345, NA, 4.536, 3.332, 2.82, 5.945, 7.067, 2.68,
                                6.51, 6.356, 6.841, 2.21), 
                      y1964 = c(4.059, 6.78782143731601,
                                7.304, 6.52552945628155, 7.036, 5.849, NA, 7.05309784978593,
                                6.573, 3.08, 4.15, NA, 4.484, 3.146, 2.79, 5.888, 7.09, 2.71,
                                6.565, 6.419, 6.837, 2.19)), 
                 row.names = c(NA, -22L), class = "data.frame")

suppressMessages(library(data.table));
suppressMessages(library(tidyverse))
setDT(DT1)
str(DT1)
#> Classes 'data.table' and 'data.frame':   22 obs. of  7 variables:
#>  $ country_name: chr  "Aruba" "Africa Eastern and Southern" "Afghanistan" "Africa Western and Central" ...
#>  $ country_code: chr  "ABW" "AFE" "AFG" "AFW" ...
#>  $ y1960       : num  4.82 6.72 7.28 6.46 6.71 ...
#>  $ y1961       : num  4.66 6.74 7.28 6.47 6.79 ...
#>  $ y1962       : num  4.47 6.76 7.29 6.49 6.87 ...
#>  $ y1963       : num  4.27 6.78 7.3 6.51 6.95 ...
#>  $ y1964       : num  4.06 6.79 7.3 6.53 7.04 ...
#>  - attr(*, ".internal.selfref")=<externalptr>

myplot <- function(my_country){
  
  cty <-  DT1[country_code == my_country]
  nation <- paste(as.character(cty[1, 1]), "Birthrate")
  
  cty_names <- names(cty) 
  cty_names <- cty_names[3:length(cty_names)] 
  cty_names <-  sub(".", "", cty_names) |> as.numeric()
  
  cty_br <- unlist(cty[1,])
  cty_br <- cty_br[3:length(cty_br)] |> as.numeric()
  
  target <- data.table(cty_names, cty_br) 
  
  ggplot(target, aes(cty_names, cty_br)) +
    geom_point(colour = "red") + geom_line(group = 1, colour ="blue")  +
    labs(title = nation, x = "Years", y = "Birthrate")
  
}

myplot("ABW")

myplot(ABW)
#> Error in eval(expr, envir, enclos): object 'ABW' not found

Created on 2024-12-28 with reprex v2.1.1

1 Like

@ FJCC
I could have sworn I did the same but yours works and mine does not.

Tomorrow I'll have to compare the codes and see where I went wrong.

Thanks to you and @ prubin..It's just a little hobby project but it was driving me mad.

This topic was automatically closed 7 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.