[R] substitute values in a vector,dataframe

Philippe Grosjean phgrosje at ulb.ac.be
Thu May 31 13:52:11 CEST 2001


On Thu, 31 May 2001, Frank Gerrit Zoellner wrote:

>> Hi!
>>
>> I've got a dataframe with -Inf values inside and i want to substitute
>>them with 0. The dataframe is only one column. How can I do this in R?

Prof Brian D Ripley wrote:

>df[df[, 1] %in% -Inf, 1] <- 0

>as in

>df <- data.frame(a=c(1:3, -Inf, 4))
>df[df[, 1] %in% -Inf, 1] <- 0
>df
>  a
>1 1
>2 2
>3 3
>4 0
>5 4

>If there were more than one column, use lapply.

What about:

b <- as.matrix(a)
b[b == -Inf] <- 0

and then, if you really need a dataframe:

a <- as.data.frame(b)

This work also with more than one column in the dataframe... but only with
numeric columns.

Philippe Grosjean


...........]<(({?<...............<?}))><...............................
 ) ) ) ) )	 __               	 __
( ( ( ( ( 	|__)              	|  _
 ) ) ) ) )	|   hilippe       	|__)rosjean
( ( ( ( ( 	Marine Biol. Lab., ULB, Belgium
 ) ) ) ) )	                  	 __
( ( ( ( ( 	|\  /|            	|__)
 ) ) ) ) )	| \/ |ariculture &	|__)iostatistics
( ( ( ( (
 ) ) ) ) )	e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org
( ( ( ( ( 	SciViews project coordinator (http://www.sciviews.org)
 ) ) ) ) )      tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home)
( ( ( ( (
 ) ) ) ) )      "I'm 100% confident that p is between 0 and 1"
( ( ( ( (                                  L. Gonick & W. Smith (1993)
 ) ) ) ) )
.......................................................................


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list