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.