[R] replacing values of matrix with random values of another dataframe

arun smartpink111 at yahoo.com
Mon Jun 11 23:31:25 CEST 2012


Hi,

Not sure if this is what you want.

 dat1<-matrix(sample(1:7,70,replace=TRUE),ncol=10,byrow=FALSE)
 dat2<-matrix(sample(1:7,70,replace=TRUE),ncol=7,byrow=FALSE)


 dat1[dat1[,1:10]==1]<-NA
> dat1
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    3    4    3    2    3    7    6    2    4     3
[2,]    2    5   NA    7    2    4    2    5    4     6
[3,]    7   NA    2    2    5    7    2    6    7     3
[4,]   NA    5    2    5   NA    6    5    5   NA     7
[5,]    5    5    6    5    7    6    4    4    7     6
[6,]    5    7    5   NA    6    5    3    6    3     2
[7,]    3    7   NA    3    2    4    7    4    5     7




 dat1[is.na(dat1)]<-sample(dat2[,1:7])



 dat1
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    3    4    3    2    3    7    6    2    4     3
[2,]    2    5    1    7    2    4    2    5    4     6
[3,]    7    7    2    2    5    7    2    6    7     3
[4,]    1    5    2    5    1    6    5    5    1     7
[5,]    5    5    6    5    7    6    4    4    7     6
[6,]    5    7    5    3    6    5    3    6    3     2
[7,]    3    7    4    3    2    4    7    4    5     7

A.K.








----- Original Message -----
From: Curtis Burkhalter <curtisburkhalter at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, June 11, 2012 3:44 PM
Subject: [R] replacing values of matrix with random values of another dataframe

Hello,

I'm having trouble performing a certain function within R and I was hoping
someone might be able to help.  I have a matrix (1000x21) that contains
whole-number values ranging from 1-7 and I want to replace all entries
within this matrix that have a value of 1 with a random number contained
within a different dataframe that has 21 rows,1000 columns.  I've tried
using the if/else function, but this always seems to return a list with
strange output.  I've also tried using the "replace" and "apply" functions,
but can't seem to get anything to work. My code is as follows:

#create data frame of 21000 random values

m_good_initial=rnorm(21000,2.79,0.18)
m_good_D1=ifelse(m_good_initial<0,0,m_good_initial)
m_good_D1mat=matrix(m_good_D1,byrow=T,21)
m_good_D1=as.data.frame(m_good_D1mat)

#create matrix of (1000x21) that contains whole-number values ranging from
1-7
#using sample to select values with a respective probability
search_strat_good <-
sample(landscenarios,1000,replace=T,prob=com_avgpgood[1,])
for (i in 2:21) {

        search_strat_good <-
cbind(search_strat_good,sample(landscenarios,1000,replace=T,prob=com_avgpgood[i,]))
}

#replace all search strategies of value "1" within matrix
"search_strat_good"
#with a random value from dataframe "m_good_D1"

bengood1=ifelse(search_strat_good==1,sample(m_good_D1,replace=F),search_strat_good)

Any help would be greatly appreciated.
-- 
Curtis Burkhalter

    [[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.




More information about the R-help mailing list