Unexpected Token Issue?

Hi all,

New R user here. I keep getting red underlines telling me there are token issues in my code. But what I have wrote does work the way it is supposed to but I don't understand why it is getting flagged. It occurs on code that I use to specify dates (mostly the last bit of code). I'm using some packages to support that code, find them below.

I'm worried I'm doing something wrong so I want to check.

Here is the whole code:

# Bringing in / Checking the dataset ----
#bringing in new packages
# importing the data from a clean excel file

m_data <- read_excel("C:/Users/andersonke/Desktop/R Data/00 BEACH DATA/Beach_Lab_Data_08222019_CLEAN.xlsx", 
                                            col_types = c("date", "text", "numeric", 
                                                          "text", "numeric", "numeric"))


# looking at the structure


#using the dplyr and ggplot2 packs to make plots but first using mutate to make a log transformation

m_data <- m_data %>%
  mutate(L10_GM = log10(GM))

#converting to a r compatible date format
m_data$DATE <- as.Date(m_data$DATE, format = '%Y-%m-%d')

#checking to make sure that the var names are present in the m_data dataset


#finding duplicates in the whole dataset
duptable <- table(m_data$BEACH, m_data$DATE)
write.csv(duptable, "duptable.csv")

#removing duplicates based on date and beach
m_data %>% distinct(BEACH, GM, DATE, .keep_all = TRUE)

# Descp Stats ----

# Basic box plot
bp <- ggplot(m_data, aes(x=BEACH, y=L10_GM))+
# Horizontal box plot
bp + coord_flip()

#Freq table of BAV and BEACH BAV=0 means no exceedance
bav.freq <- table(m_data$BAV, m_data$BEACH)
write.csv(bav.freq, "bavfreq.csv")
bav.freq <- table(m_data$BAV, m_data$BEACH, format.Date(DATE, %m)=="5")
write.csv(bav.freq, "bavfreq.csv")

#making datasets based on BAV status

m_data_BAV1 <- m_data[(m_data$GM >= 1000), ]

m_data_BAV0 <- m_data[(m_data$GM < 1000), ]

#making sub-datasets by date range (years)
mdata19 <- subset(m_data, DATE >= "2019-01-01" & DATE <= "2019-12-31")
mdata18 <- subset(m_data, DATE >= "2018-01-01" & DATE <= "2018-12-31")
mdata17 <- subset(m_data, DATE >= "2017-01-01" & DATE <= "2017-12-31")
mdata16 <- subset(m_data, DATE >= "2016-01-01" & DATE <= "2016-12-31")
mdata15 <- subset(m_data, DATE >= "2015-01-01" & DATE <= "2015-12-31")
write.csv(mdata19, "mdata19.csv")
write.csv(mdata18, "mdata18.csv")
write.csv(mdata17, "mdata17.csv")
write.csv(mdata16, "mdata16.csv")
write.csv(mdata15, "mdata15.csv")

#Making sub-datasets by date range (months)
mdata05yy <- subset(m_data, format.Date(DATE, "%m")=="05")
mdata06yy <- subset(m_data, format.Date(DATE, "%m")=="06")
mdata07yy <- subset(m_data, format.Date(DATE, "%m")=="07")
mdata08yy <- subset(m_data, format.Date(DATE, "%m")=="08")
mdata09yy <- subset(m_data, format.Date(DATE, "%m")=="09")
write.csv(mdata05yy, "mdata05yy.csv")
write.csv(mdata06yy, "mdata06yy.csv")
write.csv(mdata07yy, "mdata07yy.csv")
write.csv(mdata08yy, "mdata08yy.csv")
write.csv(mdata09yy, "mdata09yy.csv")

Nothing gets flagged when I copy your code into RStudio. Since the code runs, I am not surprised. Where exactly do you see the "error"?

Here is an image

See image in my reply below. It happens on lines 51 and 76-85.

Line 51 is

bav.freq <- table(m_data$BAV, m_data$BEACH, format.Date(DATE, %m)=="5")

My Rstudio does not flag that but the %m should be in quotes.

bav.freq <- table(m_data$BAV, m_data$BEACH, format.Date(DATE, "%m")=="5")

Try changing that and see if the problem goes away.

1 Like

I've filed an RStudio issue here to help ensure the diagnostics system diagnoses this more accurately in the future. Thanks for reporting!

1 Like

Cool thank you Kevin

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