An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20070904/12b678e4/attachment.pl
variable format
7 messages · Cory Nissen, Gabor Grothendieck, Frank E Harrell Jr +1 more
2 days later
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20070907/25d028d1/attachment.pl
A matrix is for situations where every element is of the same class but your columns have different classes so use a data frame: DF <- data.frame(a = 11:15, b = letters[1:5], stringsAsFactors = FALSE) subset(DF, a %in% 11:13) subset(DF, a %in% c(0, 11:13)) # same Suggest you review the Introduction to R manual and look at ?data.frame, ?subset and ?"%in%"
On 9/4/07, Cory Nissen <cnissen at akoyainc.com> wrote:
Okay, I want to do something similar to SAS proc format.
I usually do this...
a <- NULL
a$divisionOld <- c(1,2,3,4,5)
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic"),
ncol=2, byrow=T)
a$divisionNew[match(a$divisionOld, divisionTable[,1])] <- divisionTable[,2]
But how do I handle the case where...
a$divisionOld <- c(0,1,2,3,4,5) #no format available for 0, this throws an error.
OR
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic",
6, "East South Central",
7, "West South Central",
8, "Mountain",
9, "Pacific"),
ncol=2, byrow=T)
There are extra formats available... this throws a warning.
Thanks
Cory
[[alternative HTML version deleted]]
______________________________________________ R-help at stat.math.ethz.ch 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.
Dear Cory,
I am not familiar with SAS, but is this what you are looking for?
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic",
6, "East South Central",
7, "West South Central",
8, "Mountain",
9, "Pacific"),
ncol=2, byrow=T)
a <- NULL
a$divisionOld <- c(0,1,2,3,4,5)
a$divisionNew <-
as.character(factor(a$divisionOld,levels=divisionTable[,1],labels=divisionTable[,2]))
a$divisionNew
[1] NA "New England" "Middle Atlantic"
[4] "East North Central" "West North Central" "South Atlantic"
Kind regards,
Martin
Cory Nissen schrieb:
Anybody?
________________________________
From: Cory Nissen
Sent: Tue 9/4/2007 9:30 AM
To: r-help at stat.math.ethz.ch
Subject: variable format
Okay, I want to do something similar to SAS proc format.
I usually do this...
a <- NULL
a$divisionOld <- c(1,2,3,4,5)
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic"),
ncol=2, byrow=T)
a$divisionNew[match(a$divisionOld, divisionTable[,1])] <- divisionTable[,2]
But how do I handle the case where...
a$divisionOld <- c(0,1,2,3,4,5) #no format available for 0, this throws an error.
OR
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic",
6, "East South Central",
7, "West South Central",
8, "Mountain",
9, "Pacific"),
ncol=2, byrow=T)
There are extra formats available... this throws a warning.
Thanks
Cory
[[alternative HTML version deleted]]
______________________________________________
R-help at stat.math.ethz.ch 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.
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20070907/5d68666b/attachment.pl
Martin Becker wrote:
Dear Cory,
I am not familiar with SAS, but is this what you are looking for?
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic",
6, "East South Central",
7, "West South Central",
8, "Mountain",
9, "Pacific"),
ncol=2, byrow=T)
How about just divisionTable <- c('New England', 'Middle Atlantic', ...)
then factor(old, 1:9, divisionTable) ?
Frank
a <- NULL a$divisionOld <- c(0,1,2,3,4,5) a$divisionNew <- as.character(factor(a$divisionOld,levels=divisionTable[,1],labels=divisionTable[,2])) a$divisionNew [1] NA "New England" "Middle Atlantic" [4] "East North Central" "West North Central" "South Atlantic" Kind regards, Martin Cory Nissen schrieb:
Anybody?
________________________________
From: Cory Nissen
Sent: Tue 9/4/2007 9:30 AM
To: r-help at stat.math.ethz.ch
Subject: variable format
Okay, I want to do something similar to SAS proc format.
I usually do this...
a <- NULL
a$divisionOld <- c(1,2,3,4,5)
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic"),
ncol=2, byrow=T)
a$divisionNew[match(a$divisionOld, divisionTable[,1])] <- divisionTable[,2]
But how do I handle the case where...
a$divisionOld <- c(0,1,2,3,4,5) #no format available for 0, this throws an error.
OR
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic",
6, "East South Central",
7, "West South Central",
8, "Mountain",
9, "Pacific"),
ncol=2, byrow=T)
There are extra formats available... this throws a warning.
Thanks
Cory
[[alternative HTML version deleted]]
______________________________________________
R-help at stat.math.ethz.ch 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.
______________________________________________ R-help at stat.math.ethz.ch 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.
Frank E Harrell Jr Professor and Chair School of Medicine
Department of Biostatistics Vanderbilt University
Frank E Harrell Jr wrote:
Martin Becker wrote:
Dear Cory,
I am not familiar with SAS, but is this what you are looking for?
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic",
6, "East South Central",
7, "West South Central",
8, "Mountain",
9, "Pacific"),
ncol=2, byrow=T)
How about just divisionTable <- c('New England', 'Middle Atlantic',
...) then factor(old, 1:9, divisionTable) ?
Frank
Of course, this solution is more elegant, but my intention was 1. to provide a solution which makes use of the exisiting object "divisionTable" 2. to reproduce the output from the working example (->conversion to character) Maybe I should have emphasized that I was quoting the existing definition of divisionTable from the original email (for the sake of providing self-contained code) and not introducing a unnecessarily complicated new definition of divisionTable. Regards, Martin
a <- NULL a$divisionOld <- c(0,1,2,3,4,5) a$divisionNew <- as.character(factor(a$divisionOld,levels=divisionTable[,1],labels=divisionTable[,2])) a$divisionNew [1] NA "New England" "Middle Atlantic" [4] "East North Central" "West North Central" "South Atlantic" Kind regards, Martin Cory Nissen schrieb:
Anybody?
________________________________
From: Cory Nissen
Sent: Tue 9/4/2007 9:30 AM
To: r-help at stat.math.ethz.ch
Subject: variable format
Okay, I want to do something similar to SAS proc format.
I usually do this...
a <- NULL
a$divisionOld <- c(1,2,3,4,5)
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic"),
ncol=2, byrow=T)
a$divisionNew[match(a$divisionOld, divisionTable[,1])] <-
divisionTable[,2]
But how do I handle the case where...
a$divisionOld <- c(0,1,2,3,4,5) #no format available for 0, this
throws an error.
OR
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5, "South Atlantic",
6, "East South Central",
7, "West South Central",
8, "Mountain",
9, "Pacific"),
ncol=2, byrow=T) There are extra formats
available... this throws a warning.
Thanks
Cory
[[alternative HTML version deleted]]
______________________________________________
R-help at stat.math.ethz.ch 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.
______________________________________________ R-help at stat.math.ethz.ch 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.