0

I am trying to plot some graphs and run some ANOVA on data I imported from a csv file. I have opened the file a text editor to double check if everything look alright, and it does: [1]: https://i.stack.imgur.com/wghHg.png Once loaded into R, I have the header set to true, and column 1 set to characters, column 2 to numeric (I have tried integer as well). If I type the name of the import (data2) into the console, it displays my data correctly. However, once I try to do a boxplot or run any type of test on it, I get a message saying object 'any of the two obejects*'not found. I have also tried saving the CSV file as TXT and load it as a CSV, to no avail. Regardless if I use the read.table or read.csv commands or import the file via the GUI Import button, R does not accept the objects. Here is a piece of the code, albeit there is virtually none:

boxplot(Treatment2~WBSF2) Error in eval(predvars, data, env) : object 'Treatment2' not found

I also tried:

> boxplot(Treatment2 ~ WBSF2, data=df)
Error in as.data.frame.default(data, optional = TRUE) : 
  cannot coerce class ""function"" to a data.frame
  • Code would help. Alot. Just like [the guide says](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – hrbrmstr Oct 21 '17 at 12:12
  • it would be appreciated if you give instance with a code or sample from your dataset. with that we can help – linkonabe Oct 21 '17 at 12:14
  • apologies. I didn't post any code because there is virtually none. Importing the file using the interface does not require code, and after that anything I try results in the mentioned "object not found" message. I have edited the post now – user3437137 Oct 21 '17 at 12:18
  • The problem is in the formula, it must be the other way around, `WBSF2 ~ Treatment2`. (With the `data`argument.) – Rui Barradas Oct 21 '17 at 12:50
  • You say you've load the data as `data2`, so why do you write `data=df` in the command? – AkselA Oct 21 '17 at 13:26

2 Answers2

1

Try:

df = read.table("data.csv", sep="," , as.is=T, header=T)

boxplot(Treatment2~WBSF2, data= df)

Try removing the text "min" from the Treatment2 (see str_replace from stringr library) column and then converting it to numeric with (as.numeric).

R. Schifini
  • 9,085
  • 2
  • 26
  • 32
  • Error in as.data.frame.default(data, optional = TRUE) : cannot coerce class ""function"" to a data.frame this is the outcome – user3437137 Oct 21 '17 at 12:24
  • Treatment2 needs to have character values, as I am analyzing three different time intervals of treatment (5 values in column 2 for each) and comparing means and variance. – user3437137 Oct 21 '17 at 12:31
  • I right clicked data2 and selected "Inspect Element" It opened a new window and found the following message in the console tab of the window: [Error] Failed to load resource: the server responded with a status of 404 (Not Found) (loading-background.png, line 0) Would this be causing the issue? – user3437137 Oct 21 '17 at 12:45
0

Here's a way to enter your data into R:

df <- data.frame(Treatment2 = c('30min', '30min', '30min', '30min', '30min', 
                                '45min', '45min', '45min', '45min', '45min', 
                                '60min', '60min', '60min', '60min', '60min'), 
                 WBSF2 = c(52, 55, 51, 48, 57, 
                           43, 45, 42, 35, 40, 
                           70, 62, 55, 59, 61))

When using boxplot(), you need to have your measured variable come before your grouping variable, like this:

boxplot(WBSF2 ~ Treatment2, data = df)
ssp3nc3r
  • 3,662
  • 2
  • 13
  • 23