这是我的第一篇关于堆栈溢出的文章,如果太多的话,我会毫不掩饰的。我在R-studios上运行这个脚本,使用的是最新版本的R。
我有一个问题,我试图运行我写的脚本,当这样做的时候,它立即停止并返回这个错误给我:
Error in `-.POSIXt`(`*tmp*`) :
unary '-' is not defined for "POSIXt" objects
但是,当“单独”运行脚本时,即在这种情况下,首先运行Q4的脚本,然后只在运行Q5的脚本之后等等。它工作没有任何问题。
这是我的剧本:
# Installing packages I will need
install.packages("dplyr")
install.packages("tidyr")
install.packages("readxl")
install.packages("psych")
install.packages("car")
# loading packages
library(dplyr)
library(tidyr)
library(readxl)
library(psych)
library(car)
# Importing the Data Frame
aqdata <- read_xlsx("D:\\tVNS - Data Folder\\AnalitiQs\\Company_Y_base_data.xlsx")
# Inspecting the head, colnames and nrows to see that everything was imported successfully
head(aqdata)
colnames(aqdata)
nrow(aqdata)
# Using distinct to compare nrows to ensure that all rows are unique
distinct(aqdata, Employee_ID)
--------------
# Q4 Step 1: # Converting the stop Month column to a date vector
aqdata$Stop_Month <- as.Date(aqdata$Stop_Month)
# Q4 Step 2 - Filtering employees by department & missing values (Q depends on if last working day is date of Stop_Month or (as I interpret) last day of work was 1 day before Stop_Month date)
workers_post28 <- aqdata %>%
filter(Department == "Operations" & is.na(Stop_Month))
# Q4 Step 3: Final Answer
num_rows4 <- nrow(workers_post28)
message <- paste("On the 28th of July 2020", num_rows4, "people worked within the Operations Department of company Y")
print(message)
--------------
# Q5 Step 1: Converting the Start Month column to a date vector
aqdata$Start_Month <- as.Date(aqdata$Start_Month)
# Q5 Step 2: Filtering employees by dep, permanent contract. & 28th of Dec 2019
workers_on28Dec2019 <- aqdata %>%
filter(Department == "Operations" & Type_of_Contract == "Permanent") %>%
# Q5 Step 3: Filtering resulting employees by condition start date before Dec 28th 2019 and Stop date after 28th of Dec 2019 (based on prev question interpretation)
filter(Start_Month < as.Date("2019-12-28") & is.na(Stop_Month) | Stop_Month > as.Date("2019-12-28"))
# Q5 Step 4: Final Answer
num_rows5 <- nrow(workers_on28Dec2019)
messageQ5 <- paste("On the 28th of Dec 2019", num_rows5, "people in the Operations department had a permanent contract")
print(messageQ5)
# Q5 Step 3.1: Because I was unsure if my result was correct, I decided to verify that no employee not satisfying my conditions were left in the dataframe.
# Filter to only include employees who started working after December 28th, 2019
before_28dec2019 <- workers_on28Dec2019 %>%
filter(Start_Month > as.Date("2019-12-28"))
# Count the number of rows in the final result
num_rowscheck <- nrow(final_result)
# Print the result
if (num_rowscheck == 0) {
print("The resulting dataframe does not contain any employees who started working before December 28th, 2019")
}
--------------
# Q6 Step 1: Calculate sum of distance and dividing by length of column (Can also use mean() function for more concise code)
AverageDistance <- sum(aqdata$Distance_Work_KM) / length(aqdata$Distance_Work_KM)
messageQ6 <- paste("The average distance to work for any employee who has ever worked for Company Y is ", AverageDistance, "KM")
print(messageQ6)
--------------
# Q7 Step 1: Filter workers by gender with conditions satisfying working on 2020-07-28 --> Creating the groups
Q7df <- aqdata %>%
# Step 1.1 Filter rows based on conditions of the question (using same interpretation from Q4 where NA in Stop_Month indicates that they were working on the 28th as this is their last working day)
filter(Start_Month < as.Date("2020-07-28") & is.na(Stop_Month)) %>%
# Step 1.2 Group by gender
group_by(Gender) %>%
# Step 1.3 Select Salary column to incl into subset DF
select(Gender,Salary)
# Q7 Step 2: Assumption checking first normality, then equality of variances
describe(Q7df$Salary) # Getting M, SD and Kurtosis values for Normality assumption --> Skew/Kurtosis do not fall within -2 or +2 so its violated
shapiro.test(Q7df$Salary) # Another way of testing the assumption --> Also indicates violation.
# Both test indicate that the assumption is violated but our sample is 319 so it should be okay
# Testing equality of variances
leveneTest(Q7df$Salary ~ Q7df$Gender) # The test is non-significant meaning that the assumption is not violated
# Q7 Step 3: Running the t-test
Outcome <- t.test(Salary ~ Gender, data = Q7df) # Assuming equal variances b.c leven's was non-sig.
# Q7 Step 4: Final conclusions
print(Outcome$estimate)
print(Outcome$statistic)
print(Outcome$p.value)
messageQ7 <- paste("The T-test indicates that there is significant differences between the means of Female/Male in terms of Salary")
print(messageQ7)
--------------
注解:我知道这里有很多不应该出现在脚本中的注解,但我在回答作业问题时把它们放在了那里。
问:为什么会出现此错误。
据我所知,这是因为我试图在一个对象类上使用一个操作符“-”。但我并没有试图这样做,只是日期格式是年-月-日。R似乎只有在我单独运行脚本时才能识别,因此我感到困惑。
关于为什么只有在运行整个脚本时才会发生这种情况以及如何解决这个问题,您有什么想法吗?
非常感谢那些花时间帮助我的人!
1条答案
按热度按时间fcipmucu1#
对于任何发现这一点并正在与类似问题作斗争的人。对我来说,问题是我在使用
来分隔代码块,但我没有#注解掉它,所以编译器,我猜开始了一个新的每一个----。
当我评论他们的时候,问题就解决了!