[英]How to fix 'invalid factor level'?
I can't run a mean function. 我不能运行一个卑鄙的功能。 Here is my code : 这是我的代码:
I've tried the factor(data$date) function alone successfully. 我已经成功地尝试了因子(数据$ date)功能。 The shell answers that it is made up of 890 entry of 51 levels. shell回答它由890个51级别的条目组成。
data <- read.table("R/DATA.csv", sep = ";", header = TRUE, dec = ",")
View(data)
colnames(data)[1] <- "Date"
eau <- data$"Tension"
eaucalculee <- ( 0.000616 * eau - 0.1671) * 100
data["Eau"] <- eaucalculee
tata <- data.frame("Aucun","Augmentation","Interception")
tata[1,1]<-mean(data$Eau[data$Date == levels(factor(data$Date))[1]&
data$Traitement == "Aucun"])
I would like that te first column first row of the tata dataframe to be filled with the mean but in fact I get this error message : 我希望tata数据帧的第一列第一行用均值填充,但事实上我收到此错误消息:
In `[<-.factor`(`*tmp*`, iseq, value = 8.6692) :
invalid factor level, NA generated
Could you help me please ? 请问你能帮帮我吗 ?
You may find the csv file there : https://drive.google.com/file/d/1zbA25vajouQ4MiUF72hbeV8qP9wlMqB9/view?usp=sharing 您可以在那里找到csv文件: https : //drive.google.com/file/d/1zbA25vajouQ4MiUF72hbeV8qP9wlMqB9/view? usp =sharing
Thank you very much 非常感谢你
tata是一个因子data.frame,你想在try中插入一个数字
tata <- data.frame("Aucun","Augmentation","Interception" ,stringsAsFactors = F)
I'm not sure the line tata <- data.frame("Aucun","Augmentation","Interception")
does what you expected. 我不确定行tata <- data.frame("Aucun","Augmentation","Interception")
是否符合您的预期。 If you inspect its result with View(tata)
you will see a data frame with one record and 3 columns whose values are your 3 strings (converted to factors, as @s-brunel said). 如果使用View(tata)
检查其结果,您将看到一个数据框,其中包含一条记录和3列,其值为 3个字符串(转换为因子,如@ s-brunel所说)。 The column names were inferred from their values ( X.Aucun.
, etc). 列名是从它们的值( X.Aucun.
等)推断出来的。 I guess you rather wanted to create a data frame whose column names are the given strings. 我想你更想要创建一个数据框,其列名是给定的字符串。
Suggested code, with comments 建议的代码,带注释
data <- read.table("R/DATA.csv", sep = ";", header = TRUE, dec = ",")
# The following is useless since first column is already named Date
# colnames(data)[1] <- "Date"
# No need to create your intermediate variables eau and eaucalculee: you can
# do it directly with the data frame columns
data$Eau <- ( 0.000616 * data$Tension - 0.1671) * 100
# No need to create your tata data frame before filling its actual content, you
# can do it directly
tata <- data.frame(
Aucun = mean(data$Eau[
data$Date == levels(factor(data$Date))[1] & data$Traitement == "Aucun"
])
)
tata$Augmentation = your_formula_here
tata$Interception = your_formula_here
Note 1 : The easiest way to reference a data frame column is with $
and you don't need to use any double quotes. 注1 :引用数据框列的最简单方法是使用$
,您不需要使用任何双引号。 You can also use [[
with the double quotes (equivalent), but beware of [
which will return a data frame with a single column: 您也可以使用[[
使用双引号(等效),但要注意[
将返回带有单列的数据框:
class(data$Date)
# [1] "factor"
class(data[["Date"]])
# [1] "factor"
class(data["Date"])
# [1] "data.frame"
class(data[ , "Date"])
# [1] "factor"
Note 2 : Trying to reverse-engineer your code beyond the question you asked, maybe you want to compute the mean value of Eau for each combination of Date and Traitement. 注意2 :尝试对您提出的问题进行逆向工程,也许您想为每个Date和Traitement组合计算Eau的平均值。 In this case, I would suggest you dplyr
and tidyr
from the awesome set of packages tidyverse
: 在这种情况下,我建议你dplyr
和tidyr
从令人敬畏的tidyverse
包:
# install.packages("tidyverse") # if you don't already have it
library(tidyverse)
data <- data %>%
mutate(Eau = ( 0.000616 * data$Tension - 0.1671) * 100)
tata_vertical <- data %>%
group_by(Date, Traitement) %>%
summarise(mean_eau = mean(eau))
View(tata_vertical)
tata <- tata_vertical %>% spread(Traitement, mean_eau)
View(tata)
A lot of documentation on https://www.tidyverse.org/learn/ 关于https://www.tidyverse.org/learn/的大量文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.