簡體   English   中英

在 R 中讀取 csv 文件

[英]Reading a csv file in R

我正在嘗試從 csv 文件中讀取一些用 R 編寫的數據,而不是使用數據框。

我使用read.csv從文件中提取數據,但我總是In read.table(file = file, header = header, sep = sep, quote = quote, : line 1 appears to contain embedded nulls遇到這個錯誤In read.table(file = file, header = header, sep = sep, quote = quote, : line 1 appears to contain embedded nulls

這是 R 中使用數據框的一段代碼:

COL <- c("Fallow" = "slategray", "Mustard" = "red3" , "Mix4" = "orchid3", "Mix12"= "orange4")
SHP <- c("Fallow"=21,"Mustard"=22,"Mix4"=23, "Mix12"=24)


# generate data frame with original data
data <- data.frame(cc_variant = structure(c(1L, 1L, 1L, 3L, 4L, 2L, 3L, 4L, 2L, 2L, 4L, 3L), .Label = c("Fallow", "Mustard", "Mix4", "Mix12"), class = "factor"),
                   Date = structure(c(17092, 17093, 17098, 17092, 17092, 17092, 17093, 17093, 17093, 17098, 17098, 17098), class = "Date"),
                   NEE = c(52.3186092, 36.752742, 34.590816, -516.868370737168, -617.110003978854, -182.24567563611, -102.63776100067, -431.558870280712, -139.041211720174, -114.099387563412, -400.212603947375, -175.332083704246)
)


lm_NEE <- lmer(NEE ~ cc_variant + (1|Date), data=data)
df_NEE <- cld(emmeans(lm_NEE, specs ="cc_variant"), Letters=letters, sort=FALSE)

# Plot for BFS
ggplot(data, aes(x= cc_variant, y=NEE, fill= cc_variant))+
  geom_boxplot()+
  scale_fill_manual(values = COL, guide=FALSE)+
  geom_text(data= df_NEE ,aes(y=-600,x=cc_variant, label=.group))+
  labs(x="Catch crop variant",  y=expression("NEE (mg CO"[2]~"- C"~m^{-2}~h^{-1}~")"), fill="")+
  theme_myBW

ggsave("Fig1.png", width = 84, height = 70, units = "mm", dpi = 600)

這是數據框的外觀:

 cc_variant       Date        NEE
1      Fallow 2016-10-18   52.31861
2      Fallow 2016-10-19   36.75274
3      Fallow 2016-10-24   34.59082
4        Mix4 2016-10-18 -516.86837
5       Mix12 2016-10-18 -617.11000
6     Mustard 2016-10-18 -182.24568
7        Mix4 2016-10-19 -102.63776
8       Mix12 2016-10-19 -431.55887
9     Mustard 2016-10-19 -139.04121
10    Mustard 2016-10-24 -114.09939
11      Mix12 2016-10-24 -400.21260
12       Mix4 2016-10-24 -175.33208

這是我的嘗試:

# set vector with colors and label
COL <- c("Fallow" = "slategray", "Mustard" = "red3" , "Mix4" = "orchid3", "Mix12"= "orange4")
SHP <- c("Fallow"=21,"Mustard"=22,"Mix4"=23, "Mix12"=24)


# generate data frame with original data
data <- read.csv("C:\\Users\\aganfoud\\Desktop\\orkg-stencila\\Gentsch2020\\data.csv", header=T, sep = ';')
datalm_NEE <- lmer(NEE ~ cc_variant + (1|Date), data=data)
df_NEE <- cld(emmeans(lm_NEE, specs ="cc_variant"), Letters=letters, sort=FALSE)

# Plot for BFS
  fig1 <- ggplot(data, aes(x= cc_variant, y=NEE, fill= cc_variant))+
  geom_boxplot()+
  scale_fill_manual(values = COL, guide=FALSE)+
  geom_text(data= df_NEE ,aes(y=-600,x=cc_variant, label=.group))+
  labs(x="Catch crop variant",  y=expression("NEE (mg CO"[2]~"- C"~m^{-2}~h^{-1}~")"), fill="")+
  theme_myBW

ggsave("Fig1.png", width = 84, height = 70, units = "mm", dpi = 600)

最后這就是我創建 csv 文件的方式:

CSV文件

如果您嘗試更改是否有幫助:

data <- read.csv("C:\\Users\\aganfoud\\Desktop\\orkg-stencila\\Gentsch2020\\data.csv", header=T, sep = ';')

data <- read.csv("C:\\Users\\aganfoud\\Desktop\\orkg-stencila\\Gentsch2020\\data.csv", header=T, sep = ';',fileEncoding="utf-16")

更新:

嘗試將read.csv()更改為read_csv()

問題是文件被保存為 csv 文件但沒有導出到 csv。 所以導出對於文件的編碼是必要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM