[R] Quick R syntax question
Luke Miller
millerlp at gmail.com
Mon Jun 20 17:47:54 CEST 2011
If we assume that your data are in a data frame (which doesn't allow
spaces in column names, hence the periods in the call below):
>df = data.frame(Major.Gleason = c(4,5,2,3), Minor.Gleason = c(3,2,4,3))
You can paste together the contents of the two columns with a plus
sign in between using the paste() function. The sep='' option at the
end of the function call specifies that no spaces should be included
between pasted items.
>output = paste(as.character(df [,'Major.Gleason']), '+', as.character(df[ ,'Minor.Gleason']), sep='')
The new object 'output' is a character vector containing the 4 strings
you're after:
> print(output)
[1] "4+3" "5+2" "2+4" "3+3"
On Mon, Jun 20, 2011 at 11:31 AM, Ben Ganzfried <ben.ganzfried at gmail.com> wrote:
>
> Hi --
>
> I had a pretty quick R question since unfortunately I have not been able to
> find an answer on Google. It shouldn't take much more than a minute to
> answer.
>
> I'm trying to add up the major gleason grade and minor gleason grade for an
> analysis of patients with prostate cancer. One column has values under
> "Major Gleason" and another column has values under "Minor Gleason." For
> example,
> Major Gleason Minor Gleason
> 4 3
> 5 2
> 2 4
> 3 3
>
> I want my output to be:
> "4+3"
> "5+2"
> "2+4"
> "3+3"
>
> The quasi-pseudocode in Java is basically:
>
> major = column$majorGleason
> minor = column$minorGleason
> for item in len(Major Gleason) {
> string s = major(item) "+" minor(item);
> }
> return s;
>
> But trying the same idea in R:
>
> string <- major "+" minor
>
> gives me an error: "unexpected string constant in..."
>
> I would greatly appreciate any help.
>
> Thanks,
>
> Ben
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
___________________________
Luke Miller
Postdoctoral Researcher
Marine Science Center
Northeastern University
Nahant, MA
(781) 581-7370 x318
More information about the R-help
mailing list