tags:

views:

130

answers:

2

1) long to wide question:

I have a dataset with 3 columns: person, event, frequency. If the frequency is zero, the row is not in the table. Is there a simple way using basic R functions or libraries to convert this table to wide format, with one row per person and one column per event with the frequency as the value in table.

2) rattle question:

On a related note, is this even necessary for Rattle to understand as an input?

I am trying to import some data into R to explore some of Rattle's machine learning algorithms.

Thanks! Patrick McCann

A: 

Re: the first part, you can filter out the rows with Frequency == 0 by

filteredRows <- data$Frequency != 0
## restrict ourselves to looking at the data where Frequency != 0.
data[filteredRows,]
Aidan Cully
+3  A: 

For the first part of your question, you can either use the reshape() function that's in base R or use the reshape package (with the cast() function).

Here's an example of using the reshape() function (from the help file):

wide <- reshape(Indometh, v.names="conc", idvar="Subject",
              timevar="time", direction="wide")

Here's a simple example of using melt and cast (from the reshape help):

library(reshape)
names(airquality) <- tolower(names(airquality))
aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE)    
cast(aqm, month ~ variable, mean)
Shane