0

I am trying to read some data written in R from a csv file instead using the data frame.

I used read.csv to extract the data from the file but I got always this error In read.table(file = file, header = header, sep = sep, quote = quote, : line 1 appears to contain embedded nulls .

This is a piece of the code in R using the data frame:

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)

This how the data frame looks:

 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

This is my try:

# 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)

and finally this is how I create my csv file:

CSV File

Anouar
  • 191
  • 4
  • 14
  • 2
    Which line of code gives you that error message? – G5W Nov 03 '20 at 22:51
  • 1
    I suspect there's a mismatched quotation mark in there somewhere, but it's difficult to tell. Can you edit the question - I don't think we need anything after "This is my try", and indicate where the error occurs. – neilfws Nov 03 '20 at 22:54
  • @G5W the line is "data <- read.csv"... – Anouar Nov 03 '20 at 22:57
  • @neilfws the line of read the csv data, so shall I delete my try – Anouar Nov 03 '20 at 22:58
  • I edit my post, because it is a new error which appears – Anouar Nov 03 '20 at 23:03
  • 1
    Sounds like a [file encoding issue](https://stackoverflow.com/questions/24734911/warning-message-line-appears-to-contain-embedded-nulls/36346228). – neilfws Nov 03 '20 at 23:08
  • @neilfws I added an utf-16 enconding as suggested in the answer but it doesn't work and I am getting an error that no lines available in input – Anouar Nov 03 '20 at 23:14
  • 1
    Your file isn't necessarily utf-16. You need to work out what the encoding is. You could try `library(readr)` - install readr if required - then use `guess_encoding()`. In fact `readr::read_csv` may work better than `read.csv` - give it a try. – neilfws Nov 03 '20 at 23:17
  • @neilfws I just add the `library(readr)´and the `guess_encoding()´directly after `read.csv´but it doesn't work for me, I am getting an argument "file" is missing for guess-encoding without standard value – Anouar Nov 03 '20 at 23:25
  • 1
    You need to pass the full path and file name to `guess_encoding`, as you do for `read.csv`. Just do it in the console first, check the result. – neilfws Nov 03 '20 at 23:59
  • Thanks for your rectifying, no I have this output of encoding : `! Multiple files in zip: reading ''[Content_Types].xml'' # A tibble: 1 x 2 encoding confidence 1 ASCII 1`´ – Anouar Nov 04 '20 at 00:07

2 Answers2

1

Does it help if you try to change:

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

to

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

UPDATE:

try to changeread.csv() to read_csv()

Spyrolix
  • 40
  • 5
0

The problem was the file is saved as csv file but not exported to csv. So the exportation is necessary for the encoding of the file.

Anouar
  • 191
  • 4
  • 14