? stato filtrato un testo allegato il cui set di caratteri non era indicato... Nome: non disponibile Url: https://stat.ethz.ch/pipermail/r-help/attachments/20070312/5b30b712/attachment.pl
How to modify a column of a matrix
3 messages · Sergio Della Franca, Marc Schwartz, Bert Gunter
On Mon, 2007-03-12 at 18:55 +0100, Sergio Della Franca wrote:
Dear R-helpers, I'm trying to create a string-code to modify the contents of a column of a matrix. For example, I have this dataset: YEAR PRODUCTS 1992 3253 1993 4144 1994 3246 1996 4144 1997 4087 1998 3836 1999 4379 2000 4072 2001 4202 2002 4554 2003 4456 2004 4738 2005 4144 I want to convert/update the values of the column "PRODUCTS" under some condition (i.e. when the values of PRODUCTS is greather than 4000 replace the values of PRODUCTS whit 0 else replace with 1). My question is the following: there is a function or a metodology that allow to makes this operation? Thank you in advance, Sergio
If the data is above is matrix (MAT) and not a data frame: # See ?cbind and ?ifelse MAT <- cbind(MAT, NewCol = ifelse(MAT[, "PRODUCTS"] > 4000, 0, 1))
MAT
YEAR PRODUCTS NewCol 1 1992 3253 1 2 1993 4144 0 3 1994 3246 1 4 1996 4144 0 5 1997 4087 0 6 1998 3836 1 7 1999 4379 0 8 2000 4072 0 9 2001 4202 0 10 2002 4554 0 11 2003 4456 0 12 2004 4738 0 13 2005 4144 0 If it is a data frame: DF$NewCol <- ifelse(DF$PRODUCTS > 4000, 0, 1)
DF
YEAR PRODUCTS NewCol 1 1992 3253 1 2 1993 4144 0 3 1994 3246 1 4 1996 4144 0 5 1997 4087 0 6 1998 3836 1 7 1999 4379 0 8 2000 4072 0 9 2001 4202 0 10 2002 4554 0 11 2003 4456 0 12 2004 4738 0 13 2005 4144 0 HTH, Marc Schwartz
?cut ## if you have several bins, where ifelse becomes messy Bert Gunter Genentech Nonclinical Statistics South San Francisco, CA 94404 650-467-7374 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Marc Schwartz Sent: Monday, March 12, 2007 11:25 AM To: Sergio Della Franca Cc: r-help at stat.math.ethz.ch Subject: Re: [R] How to modify a column of a matrix
On Mon, 2007-03-12 at 18:55 +0100, Sergio Della Franca wrote:
Dear R-helpers, I'm trying to create a string-code to modify the contents of a column of a matrix. For example, I have this dataset: YEAR PRODUCTS 1992 3253 1993 4144 1994 3246 1996 4144 1997 4087 1998 3836 1999 4379 2000 4072 2001 4202 2002 4554 2003 4456 2004 4738 2005 4144 I want to convert/update the values of the column "PRODUCTS" under some condition (i.e. when the values of PRODUCTS is greather than 4000 replace the values of PRODUCTS whit 0 else replace with 1). My question is the following: there is a function or a metodology that allow to makes this operation? Thank you in advance, Sergio
If the data is above is matrix (MAT) and not a data frame: # See ?cbind and ?ifelse MAT <- cbind(MAT, NewCol = ifelse(MAT[, "PRODUCTS"] > 4000, 0, 1))
MAT
YEAR PRODUCTS NewCol 1 1992 3253 1 2 1993 4144 0 3 1994 3246 1 4 1996 4144 0 5 1997 4087 0 6 1998 3836 1 7 1999 4379 0 8 2000 4072 0 9 2001 4202 0 10 2002 4554 0 11 2003 4456 0 12 2004 4738 0 13 2005 4144 0 If it is a data frame: DF$NewCol <- ifelse(DF$PRODUCTS > 4000, 0, 1)
DF
YEAR PRODUCTS NewCol 1 1992 3253 1 2 1993 4144 0 3 1994 3246 1 4 1996 4144 0 5 1997 4087 0 6 1998 3836 1 7 1999 4379 0 8 2000 4072 0 9 2001 4202 0 10 2002 4554 0 11 2003 4456 0 12 2004 4738 0 13 2005 4144 0 HTH, Marc Schwartz ______________________________________________ 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.