character/numeric variables

1,645 views
Skip to first unread message

bruno pitton

unread,
Aug 28, 2014, 8:05:53 PM8/28/14
to davi...@googlegroups.com
My R expertise is limited but I'm trying to use a linear regression model for two variables I have.  I am getting this error:
Error in model.frame.default(formula = raw$flow ~ raw$level, drop.unused.levels = TRUE) : 
  invalid type (NULL) for variable 'raw$level'

and don't know why.  Searching online, it seems that this error usually indicates a non-numeric variable/value.  Using is.numeric(raw$level), is.character, is.matrix, is.integer, is.vector they are all false.  I tried converting the column to numbers using as.numeric(raw$level) but it still doesn't work.  I think it has something to do with importation of data but not sure.  I import excel based .csv files with two columns and single word headers for simplicity.  There are no letters or non-period punctuation in the values. 

I hope it's something simple I am overlooking, which is easy considering my lack of R skills.

Thanks in advance.
Bruno

Noam Ross

unread,
Aug 28, 2014, 8:12:32 PM8/28/14
to Davis R Users Group

Bruno,

Could you provide the output of str(raw)? It will give the names and types of each of the variables in your data.

Noam



--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Rosemary Hartman

unread,
Aug 29, 2014, 9:58:18 AM8/29/14
to davi...@googlegroups.com
You can also get the type of a specific variable by using:

class(raw$level)

Here's a good resource for learning about how to deal with different data types if you haven't found it already: http://www.statmethods.net/input/datatypes.html

Given that your error message says "invalid type (NULL)", it appears that it cannot find your variable at all. That usually happens to me when I have made a spelling mistake somewhere...

In general when posting questions to this listserve, it's really helpful for us if you can show us as much of the code leading up to the error as possible. In this case, what did the code for the linear regression itself look like? The "drop.unused.levels" command is usually used for factors, so R might think it's a factor rather than numeric data.

Hope that helps
Rosie


On Thu, Aug 28, 2014 at 5:05 PM, bruno pitton <bruno...@gmail.com> wrote:

--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.



--

bruno pitton

unread,
Sep 2, 2014, 11:43:27 AM9/2/14
to davi...@googlegroups.com
Rosie and Noam,
You both pointed me in the right direction.  I used str(raw) and found that I capitalized Level, so raw$level should have been raw$Level, so a spelling error.

I needed to step away and come back to it.

Thanks for your help.
Reply all
Reply to author
Forward
0 new messages