JHowIX
JHowIX

Reputation: 1803

Input to Levels Function - R

I have an Rscript that creates various 2D charts on a dataset. I'd like to be able to change the X variable depending on which relationship I want to view. So I made the X variable a command line argument. Motivations aside, I want to be able to run the command:

scoreData <- read.csv(..)
xVarString <- args[0]
levels(paste(scoreData$,xVarString,sep=""))

but it simply returns NULL. I know paste returns a string. I know levels() does not accept a string. What I dont know is the type that the levels function accepts. I have tried

levels(as.vector(paste(scoreData$,xVarString,sep="")))
levels(as.list(paste(scoreData$,xVarString,sep="")))
levels(as.data.frame(paste(scoreData$,xVarString,sep="")))

As a general question, is there a place that shows R function input / output / documentation, similar to the .NET documentation and Java docs? I have found plenty of tutorials but no straight function documentation.

Thakns in advance.

Upvotes: 0

Views: 180

Answers (1)

Se&#241;or O
Se&#241;or O

Reputation: 17412

levels accepts vectors of class factor (the function levels(x) is really just shorthand for attr(x, "levels").

You need to pass the actual column of data to levels. "ScoreData$Column1" will only pass the string (as you point out). You could do:

levels(scoreData[,xVarString])

Assuming xVarString is a string of the desired column name. An easier method is:

sapply(scoreData, levels)

To see all the levels separated by column.

Upvotes: 1

Related Questions