Curtis Burkhalter
2012-Jun-11 19:44 UTC
[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]]
arun
2012-Jun-11 21:31 UTC
[R] replacing values of matrix with random values of another dataframe
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.
Sarah Goslee
2012-Jun-11 21:32 UTC
[R] replacing values of matrix with random values of another dataframe
Hi Curtis, This isn't quite a reproducible example, though very close. As far as I can tell, you're over-thinking it. Instead of: bengood1=ifelse(search_strat_good==1,sample(m_good_D1,replace=F),search_strat_good) bengood1 <- search_strat_good bengood1[bengood1 == 1] <- sample(m_good_D1, size=sum(bengood1 == 1), replace=FALSE) I made a couple other comments inline: On Mon, Jun 11, 2012 at 3:44 PM, Curtis Burkhalter <curtisburkhalter at gmail.com> wrote:> 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)Again, you don't need the ifelse: m_good_D1 <- m_good_initial m_good_D1[m_good_D1 < 0] <- 0> 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,])) > }Using cbind is a lot less efficient than creating a matrix of the desired size beforehand and filling in each column. Neater yet: search_strat_good <- sapply(1:21, function(i)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 >-- Sarah Goslee http://www.functionaldiversity.org