0

i have letterdata.csv data and i would like to apply classification task to this data, for this one i have read file in R

letters <- read.csv("letterdata.csv")

after that i have created test and train data

> letters_train <- letters[1:16000, ]
> letters_test <- letters[16001:20000, ]

and then apply kernel classification function

> letter_classifier <- ksvm(letter ~ ., data = letters_train,
+                           kernel = "vanilladot")

but it gives me following error

Error in eval(predvars, data, env) : object 'letter' not found

i am using book and this command was in book, so i decided that, instead of letter, letters should be so i apply following command

> letter_classifier <- ksvm(letters ~ ., data = letters_train,
+                           kernel = "vanilladot")

but it gives me another error

Error in model.frame.default(data = ..1, formula = x) : 
  invalid type (list) for variable 'letters'
> 

so what can i do?

EDIT : structure of dataset

> str(letters)
'data.frame':   400375 obs. of  1 variable:
 $ X..DOCTYPE.html.: Factor w/ 40331 levels "  ","    ","      ",..: 40294 40203 40171 40212 40207 40208 40209 40210 40211 40213 ...

and about library i am using kernlab package

Arun kumar mahesh
  • 2,289
  • 2
  • 14
  • 22
  • Avoid names like `letters`. There's already a built-in "dataset" named `letters`. Add `str(letters)` and `dput(head(letters,20))` to your question plus packages you're using. – NelsonGon Feb 27 '19 at 11:51
  • i have updated information –  Feb 27 '19 at 11:57
  • i have changed name for myletter, but the same error : invalid type list for myletter –  Feb 27 '19 at 11:59
  • Please edit to add the output of `dput(head(letters,20))` so people can help. – NelsonGon Feb 27 '19 at 12:01
  • but it gives me huge result –  Feb 27 '19 at 12:06
  • 1
    how can i post whole result? it gives me huge text messages, huge description –  Feb 27 '19 at 12:07
  • Sorry I'm unable to help further. Maybe someone else can better guide. In the meantime, you could find some tips [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – NelsonGon Feb 27 '19 at 12:11
  • Looks like your data isn't a CSV... could you post the first few rows of your file `letterdata.csv`? How many columns do you expect it to have? How many rows? From your `str(letters)` it looks like it has 400,375 rows and 1 column. From the code above it looks like you think it has 20,000 rows and multiple columns. – Gregor Thomas Feb 27 '19 at 16:17
  • here is datasets https://github.com/stedy/Machine-Learning-with-R-datasets, look please at letterdata.csv file –  Feb 27 '19 at 17:17
  • 1
    How did you download the file? Seems like maybe you saved the HTML Github page? That would explain the `X..DOCTYPE.html` in your `str`. I would recommend either (a) cloning the repository to get the file or (b) if you want to save it from the web, go to the file's page on Github but **click the `Raw` button before saving**. You want to save the raw CSV file, not wrapped in HTML for web display. – Gregor Thomas Feb 28 '19 at 14:51
  • In the future, I'd strongly recommend doing a quick check to make sure data imported correctly before moving on to modeling. Just glance at `head(your_data)` and make sure it looks like it has the rows and columns you expect. – Gregor Thomas Feb 28 '19 at 14:52

1 Answers1

0

R is automaticly saving your data as a list, you need to convert all your data into a data frame.

df <- as.data.frame(letters)
df_train <- as.data.frame(letters_train) -- this is where the error must be coming from

Then just use df instead:

letter_classifier <- ksvm(df ~ ., data = df_train,
+                           kernel = "vanilladot")
Ângelo D
  • 68
  • 1
  • 11
  • This is better left as a comment not an answer. – NelsonGon Feb 27 '19 at 11:54
  • 1
    Well, I wanted to comment but can't because reputation minimum and I prefer to have an incomplete answer and then complete it, then not help. – Ângelo D Feb 27 '19 at 11:56
  • @datodatuashvili I think this should help you now – Ângelo D Feb 27 '19 at 16:18
  • I still don't think this answer is useful. OP posted `str(letters) 'data.frame': 400375 obs. of 1 variable:` in the question showing that `letters` is a data frame, so using `as.data.frame` on it will do nothing. I'm not sure where you get the idea that it is a `list` (or that OP saved it in R). Also as it has one column not named `df`, the formula `df ~ .` will be an error. – Gregor Thomas Feb 27 '19 at 16:25
  • I appreciate that you want to help immediately, but perhaps look for clearer questions until you have enough rep to comment and request clarification. It only takes 50 rep to comment anywhere, so you're quite close. – Gregor Thomas Feb 27 '19 at 16:26
  • Yes, but the letters_train might not be, like I said in the answer, that's where it must be coming from. Because even if you take part of a dataframe it can be converted to a list, since it has 1 variable, the first one was just a too make sure :) – Ângelo D Feb 27 '19 at 17:20
  • > letter_classifier <- ksvm(df ~ ., data = df_train, + kernel = "vanilladot") Error in model.frame.default(data = ..1, formula = x) : invalid type (list) for variable 'df' > the same error –  Feb 27 '19 at 17:33
  • *"that's where it must be coming from"*. Strongly disagree. – Gregor Thomas Feb 28 '19 at 14:53