Help with listing files and splitting data

Hi R community, I've been given some code to run, but cannot get it to work properly:
focal.data <- list.files(path = "~/Masters 2021 stuff/Research apprenticeship/Group by year", pattern="*FocalData.txt")
#Split out year from the file names
year <- str_split(focal.data,pattern="_")
year <- do.call(rbind,year)
year <- year[,1]
year <- str_sub(year,(nchar(year)-3),nchar(year))
year <- as.numeric(year)

When I run this I get the following error message:
Error in as.numeric(year) :
cannot coerce type 'closure' to vector of type 'double'

Does anybody know what's going wrong? I'm confident that i've set the working directory correctly.
In the Environment section, it says focal.data character(empty), so presumably it hasn't read in my word documents.
I thought the problem might be that I was using word documents and the code says txt in the pattern. So I converted all word docs to txt and changed the file names to reflect the word documents: pattern="GroupByYear.txt"
In doing this the environment now shows:

Which looks better, but I still get the following error code when I try and split it:
Error in as.numeric(year) :
cannot coerce type 'closure' to vector of type 'double'

I have the following packages open:
library(gt)
library(plyr)
library(tidyverse)
library(data.table)
library(magrittr)
library(lubridate)
library(readr)

Any help would be much appreciated.

Your code works for me. I invented a vector of values for focal.data based on the image you provided.

focal.data <- c("GroupF2010_GroupByYear.txt", "GroupF2011_GroupByYear.txt")
year <- str_split(focal.data,pattern="_")
year <- do.call(rbind,year)
year <- year[,1]
year <- str_sub(year,(nchar(year)-3),nchar(year))
year <- as.numeric(year)
print(year)
[1] 2010 2011

The lubridate package has a function named year, so running as.numeric(year) without previously assigning a value to year will cause an error. Have you successfully run the lines before as.numeric(year)? Run them one at a time and make sure you see a value for year in the Environment pane. Or, better, change the variable name year to YEAR to avoid possible confusion with the function name.

1 Like

By the way, your code seems needlessly complicated. I might have oversimplified the text values involved, but using my example values, the four digit year can be directly extracted.

focal.data <- c("GroupF2010_GroupByYear.txt", "GroupF2011_GroupByYear.txt")
YEAR <- str_extract(focal.data, "\\d{4}") #extract the four digits from each value
YEAR <- as.numeric(YEAR)
print(YEAR)
[1] 2010 2011
1 Like

Thanks so much for your help. It's working fine now.

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.