Re: [R] replace values in a distance matrix

2006-07-27 Thread jim holtman
M4.5[M4.5 < 4.5] <- 18

On 7/27/06, Mari Carmen Garcia Esteban <[EMAIL PROTECTED]> wrote:
>
> Hi to everybody!
> I´m just a beginner in R, and I´m trying to replace values in a distance
> matrix with a concret condition: replace all values (elements) lower than
> 4.5 with value=18.
>
> I´ve tried this, but it doesn´t work...
>
> Dxy would be my 117 x 117 euclidean distance matrix
>
>M18 and M4.5 would be 117 x 117 matrices:
>
> M18<-matrix(rep(18,13689),nrow=117)
> M4.5<-matrix(rep(4.5,13689),nrow=117)
>
> REPLACE ALL VALUES >4.5 WITH 18.0 in Dxy
>
> for(i in 1:117) {
> for(j in 1:117) {
> Dxy[i,j]<-ifelse(Dxy[i,j] }
> }
>
>
> Thanks in advance!
>
> Mari Carmen Garcia
> Ud. Anatomía, Fisiologia y Génetica.
> Escuela Técnica Superior de Ingenieros de Montes de Madrid
> Spain.
>
>
>
>
>
> -
>
>
>
>
>[[alternative HTML version deleted]]
>
>
>
> __
> R-help@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.
>
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] replace values in data frame

2006-07-07 Thread Marc Schwartz (via MN)
Wade,

Given that you appear to have multiple search and replace items to deal
with, here is a possible loop based "Global Search and Replace"
solution:

gsr <- function(Source, Search, Replace)
{
  if (length(Search) != length(Replace))
stop("Search and Replace Must Have Equal Number of Items\n")

  Changed <- as.character(Source)

  for (i in 1:length(Search))
  {
cat("Replacing: ", Search[i], " With: ", Replace[i], "\n")
Changed <- replace(Changed, Changed == Search[i], Replace[i])
  }

  cat("\n")
  
  Changed
}


Source:  The source vector, which will be coerced to character

Search: The Search values to be matched in "Source" as a character
vector

Replace: The values that will replace those found in 'Search' on a
one-for-one basis.


This function returns a character vector. As with replace(), the result
must be assigned. The change is not done 'in place'. The function will
also output the search and replace values to the console during the
loop.


Again, using the iris dataset as an example:

> iris$Species
  [1] setosa setosa setosa setosa setosa setosa
  [7] setosa setosa setosa setosa setosa setosa
 [13] setosa setosa setosa setosa setosa setosa
 [19] setosa setosa setosa setosa setosa setosa
 [25] setosa setosa setosa setosa setosa setosa
 [31] setosa setosa setosa setosa setosa setosa
 [37] setosa setosa setosa setosa setosa setosa
 [43] setosa setosa setosa setosa setosa setosa
 [49] setosa setosa versicolor versicolor versicolor versicolor
 [55] versicolor versicolor versicolor versicolor versicolor versicolor
 [61] versicolor versicolor versicolor versicolor versicolor versicolor
 [67] versicolor versicolor versicolor versicolor versicolor versicolor
 [73] versicolor versicolor versicolor versicolor versicolor versicolor
 [79] versicolor versicolor versicolor versicolor versicolor versicolor
 [85] versicolor versicolor versicolor versicolor versicolor versicolor
 [91] versicolor versicolor versicolor versicolor versicolor versicolor
 [97] versicolor versicolor versicolor versicolor virginica  virginica 
[103] virginica  virginica  virginica  virginica  virginica  virginica 
[109] virginica  virginica  virginica  virginica  virginica  virginica 
[115] virginica  virginica  virginica  virginica  virginica  virginica 
[121] virginica  virginica  virginica  virginica  virginica  virginica 
[127] virginica  virginica  virginica  virginica  virginica  virginica 
[133] virginica  virginica  virginica  virginica  virginica  virginica 
[139] virginica  virginica  virginica  virginica  virginica  virginica 
[145] virginica  virginica  virginica  virginica  virginica  virginica 
Levels: setosa versicolor virginica


Note that iris$Species is a factor with specific levels. The gsr()
function above will coerce the Source argument to a character vector
internally and return a character vector:

> iris$Species <- gsr(iris$Species, 
  c("setosa", "versicolor", "virginica"), 
  c("s1", "v1", "v2"))
Replacing:  setosa  With:  s1 
Replacing:  versicolor  With:  v1 
Replacing:  virginica  With:  v2 

> iris$Species
  [1] "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1"
 [14] "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1"
 [27] "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1"
 [40] "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "s1" "v1" "v1"
 [53] "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1"
 [66] "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1"
 [79] "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1"
 [92] "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v1" "v2" "v2" "v2" "v2"
[105] "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2"
[118] "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2"
[131] "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2" "v2"
[144] "v2" "v2" "v2" "v2" "v2" "v2" "v2"


HTH,

Marc Schwartz

__
R-help@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


Re: [R] replace values in data frame

2006-07-07 Thread Marc Schwartz (via MN)
Thanks for re-posting onlist.  My offlist reply:

The 'list' argument, as per ?replace, needs to be a vector of one or
more indices into another vector, generally the source vector argument
'x', not the actual values that you want to replace. 'list' can either
be explicit integers as the indices, or the result of a logical
operation as I used in my example.

In addition, the objects 'list' and 'replace' in your code are not
vectors, but they are data frames. That is the default
type of object created when using read.table().

You can use str(list) and str(replace) to check this. The str() function
returns the structure of an object.

It is likely that you really want list$V1 and replace$V1 as the vectors
of interest here. V1 thru Vx are the default column names created when
using read.table() if no header row exists in the incoming file.

In addition, note that replace() is not vectorized. It will not handle
multiple search and replace arguments in a single call. See my example
using the iris dataset.

You would need to do each one separately. You can create a loop of one
type or another to cycle through multiple arguments if you wish, where
each cycle through the loop is a single call to replace() with a new set
of values as appropriate for the arguments.

Finally, you should generally not use R object or function names for
user created objects. Both 'list' and 'replace' are such objects. R will
generally be able to differentiate, but there is no guarantee and it
will help you avoid code debugging headaches.

HTH,

Marc Schwartz



On Fri, 2006-07-07 at 12:58 -0400, Wade Wall wrote:
> The format is like this.
> 
> Plot  species   Value
> 
> P1  ACERRUB   3
> P2  MAGNVIR2
> P3  ARONARB   2
> etc.
> 
> imported using x<-read.table(file="filename.txt")
> 
> I want to replace a list of values in the 2nd column with another list. For
> example, I want to replace ARONARB with PHOTPYR.
> list<-read.table(file="originalnames.txt")
> replace<-read.table(file="replacementnames.txt")
> I tried to use replace in this manner:
> 
> newx<-replace(x,list,replace)
> however, I get the error message: error in replace, invalid subscript type.
> 
> I have tried transforming the above list and modifying the column names
> (column 2), but to no avail.  I hope this clarifies a little.  Sorry about
> that.

> 
> >
> > On Fri, 2006-07-07 at 11:20 -0400, Wade Wall wrote:
> > > Hi all,
> > >
> > > I have a three columned list that I have imported into R.  The first
> > column
> > > is a plot (ex. Plot1), the second is a species name (ex ACERRUB) and the
> > > third a numeric value.  I want to replace some of the second column
> > names
> > > with other names (for example replace ACERRUB with ACERDRU).  The
> > original
> > > and replacement values are in separate lists (not vectors), but I can't
> > seem
> > > to find the right function to perform this.  The replace function seems
> > to
> > > only want to work with numbers.
> > >
> > > Any clues?
> > >
> > > Wade
> >

__
R-help@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


Re: [R] replace values in data frame

2006-07-07 Thread Wade Wall
The format is like this.

Plot  species   Value

P1  ACERRUB   3
P2  MAGNVIR2
P3  ARONARB   2
etc.

imported using x<-read.table(file="filename.txt")

I want to replace a list of values in the 2nd column with another list. For
example, I want to replace ARONARB with PHOTPYR.
list<-read.table(file="originalnames.txt")
replace<-read.table(file="replacementnames.txt")
I tried to use replace in this manner:

newx<-replace(x,list,replace)
however, I get the error message: error in replace, invalid subscript type.

I have tried transforming the above list and modifying the column names
(column 2), but to no avail.  I hope this clarifies a little.  Sorry about
that.




On 7/7/06, Marc Schwartz (via MN) <[EMAIL PROTECTED]> wrote:
>
> On Fri, 2006-07-07 at 11:20 -0400, Wade Wall wrote:
> > Hi all,
> >
> > I have a three columned list that I have imported into R.  The first
> column
> > is a plot (ex. Plot1), the second is a species name (ex ACERRUB) and the
> > third a numeric value.  I want to replace some of the second column
> names
> > with other names (for example replace ACERRUB with ACERDRU).  The
> original
> > and replacement values are in separate lists (not vectors), but I can't
> seem
> > to find the right function to perform this.  The replace function seems
> to
> > only want to work with numbers.
> >
> > Any clues?
> >
> > Wade
>
> Without seeing the code you are using, we can only guess a syntax error
> of some sort.  It works fine using the iris dataset:
>
> > head(iris)
>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
> 1   5.1 3.5  1.4 0.2  setosa
> 2   4.9 3.0  1.4 0.2  setosa
> 3   4.7 3.2  1.3 0.2  setosa
> 4   4.6 3.1  1.5 0.2  setosa
> 5   5.0 3.6  1.4 0.2  setosa
> 6   5.4 3.9  1.7 0.4  setosa
> 7   4.6 3.4  1.4 0.3  setosa
> 8   5.0 3.4  1.5 0.2  setosa
> 9   4.4 2.9  1.4 0.2  setosa
> 10  4.9 3.1  1.5 0.1  setosa
>
> > iris$Species <- replace(iris$Species, iris$Species == "setosa",
>  "NewValue")
>
> > head(iris)
>   Sepal.Length Sepal.Width Petal.Length Petal.Width  Species
> 1   5.1 3.5  1.4 0.2 NewValue
> 2   4.9 3.0  1.4 0.2 NewValue
> 3   4.7 3.2  1.3 0.2 NewValue
> 4   4.6 3.1  1.5 0.2 NewValue
> 5   5.0 3.6  1.4 0.2 NewValue
> 6   5.4 3.9  1.7 0.4 NewValue
> 7   4.6 3.4  1.4 0.3 NewValue
> 8   5.0 3.4  1.5 0.2 NewValue
> 9   4.4 2.9  1.4 0.2 NewValue
> 10  4.9 3.1  1.5 0.1 NewValue
>
>
> HTH,
>
> Marc Schwartz
>
>
>

[[alternative HTML version deleted]]

__
R-help@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


Re: [R] replace values in data frame

2006-07-07 Thread Marc Schwartz (via MN)
On Fri, 2006-07-07 at 11:20 -0400, Wade Wall wrote:
> Hi all,
> 
> I have a three columned list that I have imported into R.  The first column
> is a plot (ex. Plot1), the second is a species name (ex ACERRUB) and the
> third a numeric value.  I want to replace some of the second column names
> with other names (for example replace ACERRUB with ACERDRU).  The original
> and replacement values are in separate lists (not vectors), but I can't seem
> to find the right function to perform this.  The replace function seems to
> only want to work with numbers.
> 
> Any clues?
> 
> Wade

Without seeing the code you are using, we can only guess a syntax error
of some sort.  It works fine using the iris dataset:

> head(iris)
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1   5.1 3.5  1.4 0.2  setosa
2   4.9 3.0  1.4 0.2  setosa
3   4.7 3.2  1.3 0.2  setosa
4   4.6 3.1  1.5 0.2  setosa
5   5.0 3.6  1.4 0.2  setosa
6   5.4 3.9  1.7 0.4  setosa
7   4.6 3.4  1.4 0.3  setosa
8   5.0 3.4  1.5 0.2  setosa
9   4.4 2.9  1.4 0.2  setosa
10  4.9 3.1  1.5 0.1  setosa

> iris$Species <- replace(iris$Species, iris$Species == "setosa",
  "NewValue")

> head(iris)
   Sepal.Length Sepal.Width Petal.Length Petal.Width  Species
1   5.1 3.5  1.4 0.2 NewValue
2   4.9 3.0  1.4 0.2 NewValue
3   4.7 3.2  1.3 0.2 NewValue
4   4.6 3.1  1.5 0.2 NewValue
5   5.0 3.6  1.4 0.2 NewValue
6   5.4 3.9  1.7 0.4 NewValue
7   4.6 3.4  1.4 0.3 NewValue
8   5.0 3.4  1.5 0.2 NewValue
9   4.4 2.9  1.4 0.2 NewValue
10  4.9 3.1  1.5 0.1 NewValue


HTH,

Marc Schwartz

__
R-help@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


Re: [R] replace values?

2006-07-02 Thread Matthias Braeunig
# reproducing your example
xx<-"x y z
+ 1 2 3
+ 2 3 1
+ 3 2 1
+ 1 1 3
+ 2 1 2
+ 3 2 3
+ 2 1 1"

# you did not tell us the class of your data, assuming data.frame
df<-read.table(textConnection(xx),header=T,colClasses="factor")

# a clean way to do what you want is using factors with ?levels
# (note that data has already been read as factor)
levels(df$x)<-c("a","b","c","d")
levels(df$y)<-c("b","a","c","d")
levels(df$z)<-c("d","c","b","a")

subset(df,x=="a")
  x y z
1 a a b
4 a b b
subset(df,x=="a"&y=="a")
  x y z
1 a a b


HTH, m


zhijie zhang wrote:
> Dear friends,
>   i have a dataset like this:
> x y z
> 1 2 3
> 2 3 1
> 3 2 1
> 1 1 3
> 2 1 2
> 3 2 3
> 2 1 1
> I want to replace x with the following values:1<-a,2<-b,3<-c,4<-d;
>  replace y with the following values:1<-b,2<-a,3<-c,4<-d;
>  replace z with the following values:1<-d,2<-c,3<-b,4<-a;
> Finally,select two subsets:
> 1. if x='a';
> 2.x='a' and y='a';
>  thanks very much!

__
R-help@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


Re: [R] replace values?

2006-07-02 Thread Jonathan Baron
On 07/02/06 12:39, zhijie zhang wrote:
> Dear friends,
>   i have a dataset like this:
> x y z
> 1 2 3
> 2 3 1
> 3 2 1
> 1 1 3
> 2 1 2
> 3 2 3
> 2 1 1
> I want to replace x with the following values:1<-a,2<-b,3<-c,4<-d;
>  replace y with the following values:1<-b,2<-a,3<-c,4<-d;
>  replace z with the following values:1<-d,2<-c,3<-b,4<-a;

Here's one way.  Call your dataset M, and assume it is a
data.frame.  This method of replacement works best when you are
replacing consecutive integers, as you are.  Note that X[1] is
"a", X[2] is "b" and so on.

X <- c("a","b","c","d")
Y <- c("b","a","c","d")
Z <- c("d","c","b","a")
M$x <- X[M$x]
M$y <- Y[M$y]
M$z <- Z[M$z]

> Finally,select two subsets:
> 1. if x='a';
> 2.x='a' and y='a';

M[M$x=="a",]
M[M$x=="a" & M$y=="a",]

The subsets will be rows.  I'm not sure that's what you mean.

Jon
-- 
Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page: http://www.sas.upenn.edu/~baron

__
R-help@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


Re: [R] replace values in a matrix subject to boolean condition

2005-03-24 Thread Werner Wernersen
Uwe Ligges wrote:
> Werner Wernersen wrote:
> 
>> Hi everybody!
>>
>> I am sorry to bother you with a question so simple
but
>> I think there might be a better solution:
>> I have a matrix of size 360x501 where I want to
check
>> the value of each 5th column of each row and
replace it (and the 6th, 
>> 7th,
>> 8th column) by zero if the value is less than 1000.
I have written a 
>> double loop
>> to do that but that requires a lot of time.
>>
>> Is there a faster way to achieve this?
> 
> 
> 
> Two ways to interpret your question:
> 
> 1) if col5 < 100 replace col5 & col6 & col7 & col8
by 0:
> 
>X[X[,5] < 1000, 5:8] <- 0
> 
> 
> 2) if col5 < 100 replace col5 by 0, if col6 < 100
replace col6 by 0, ...:
> 
> 
> for(i in 5:8)
>X[X[,i] < 1000, i] <- 0
> 
> Uwe Ligges
> 
> 
> 
> 
> 
>> Thanks,
>>Werner
>>
>> __
>> R-help@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
> 
> 
> 
> 
Thanks for all your answers!
Now I did it with
for (j in seq(3,length(d[1,]),by=5)) {
d[d[,j]<1000,j:(j+3)] <- c(0,0,0,0)
}
which is so much faster than the double loop.

Thanks again,
   Werner

__
R-help@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


Re: [R] replace values in a matrix subject to boolean condition

2005-03-24 Thread Uwe Ligges
Werner Wernersen wrote:
Hi everybody!
I am sorry to bother you with a question so simple but
I think there might be a 
better solution:
I have a matrix of size 360x501 where I want to check
the value of each 5th 
column of each row and replace it (and the 6th, 7th,
8th column) by zero if the 
value is less than 1000. I have written a double loop
to do that but that 
requires a lot of time.

Is there a faster way to achieve this?

Two ways to interpret your question:
1) if col5 < 100 replace col5 & col6 & col7 & col8 by 0:
   X[X[,5] < 1000, 5:8] <- 0
2) if col5 < 100 replace col5 by 0, if col6 < 100 replace col6 by 0, ...:
for(i in 5:8)
   X[X[,i] < 1000, i] <- 0
Uwe Ligges


Thanks,
   Werner
__
R-help@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
__
R-help@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


Re: [R] replace values but different replacement length

2004-07-02 Thread Spencer Graves
 What are the lengths of
match02$BL02DATE[is.na(match02$BL02DATE)] and match02$BLOCKED?  

	  In particular, have you considered the following:  

match02$BL02DATE[is.na(match02$BL02DATE)] <-  match02$BLOCKED[is.na(match02$BL02DATE)]
 hope this helps.  spencer graves
Christian Schulz wrote:
Hi,
have anybody a hint/help how it's possible replace i.e.
the NA values  from BL02DATE with  non-Missing 
values from BLOCKED  and vice versa. 
Both variables are with an id  in a 188.249 rows data.frame and
my fr function didn't count the NA's.

Many thanks 
Christian

 

fr(BL02DATE)
 

  Count Prcnt
1 140660  84.6 
2  25589  15.4 
Total 166249 100.0

 

fr(BLOCKED)
 

  Count Prcnt
1 151982  85.4 
2  25976  14.6 
Total 177958 100.0

 

match02$BL02DATE[is.na(match02$BL02DATE)] <-  match02$BLOCKED
 

Warning message: number of items to replace is not a multiple of
replacement length
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
 

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html