Please help R novice with an assignment! I need to write a function, taking a single argument year, which reads data from csv files from the website. These csv files are available at the addresses like the one below where only the year part changes: 'https://www.nbp.pl/kursy/Archiwum/archiwum_tab_a_2020.csv'
I’m trying to separate URL into parts and then paste() them together, including the year input from the function. My code is below. But it causes the following error: Error in urlPart1 urlYear : non-numeric argument to binary operator
How can I overcome this error or could you advise me any other approach, maybe some regular expressions? Also I’m afraid we are not allowed to use different fancy packages.
my_func <- function(year) {
urlPart1 = 'https://www.nbp.pl/kursy/Archiwum/archiwum_tab_a_'
urlYear = year
urlPart2 = '.csv'
url = paste(urlPart1, urlYear, urlPart2, sep = “”)
d = read.table(url, header = TRUE, sep = ';', fill = TRUE)
}
print(my_func(2018))
CodePudding user response:
The problem seems to come from the line qhere you paste the url together. The quotation marks of the sep argument seem to be some local version that is not accepted. You can try:
my_func <- function(year) {
urlPart1 = 'https://www.nbp.pl/kursy/Archiwum/archiwum_tab_a_'
urlYear = year
urlPart2 = '.csv'
url = paste(urlPart1, urlYear, urlPart2, sep = '')
d = read.table(url, header = TRUE, sep = ';', fill = TRUE)
}
print(my_func(2018))