1
votes

I have a group like so

group=c("year","reach", "treatment.long", "transect")

then later in my code I need to dcast() my data based on the group (note the group will be changing).

dcast(melted.data, year + reach + treatment.long + transect ~ stat, value.var="value")

How can I get the group into the format year + reach + treatment.long + transect?

Thank you

I adjusted it to

dcast(melted.data, as.formula(paste(paste( group, collapse=' + ' ), "~ stat")), value.var="value")

and am getting the error "Aggregation function missing: defaulting to length"

1

1 Answers

2
votes

You can use paste with the collapse argument:

paste( group, collapse=' + ' )

This will create the string. You can copy and paste it to the command line in the middle of the command, or you can also use paste to add the ~ stat and pass the resulting string through as.formula. See the ?as.formula help page.