开发者

2 questions: 1) long 2 wide data in R, 2) followup re: rattle

1) long to wide question:

I have a dataset开发者_如何学JAVA 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


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)


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,]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜