[R] function with loop that goes through columns of dataframes with different dimensions

arun smartpink111 at yahoo.com
Sat Oct 5 01:51:12 CEST 2013


Hi,

May be this helps:
set.seed(24)
dat1<- as.data.frame(matrix(sample(1:50,100,replace=TRUE),10,10))
colnames(dat1)<- paste0("Col",1:ncol(dat1))

rnd.data1 <- function(x,n,ColSub,ColIndex=FALSE){
 library(matrixStats)
 if(ColIndex){
    index <- seq_len(ncol(x))%in% ColSub
  Mins1 <- colMins(x[index])
  Maxs1 <- colMaxs(x[index])
 library(data.table)
 res <- sapply(seq_along(Mins1),function(i) runif(n, Mins1[i],Maxs1[i]))
  colnames(res)<- colnames(x)[index]
res <- data.table(res) 
  }
 else{
 Mins1 <- colMins(x)
 Maxs1 <- colMaxs(x)
 res <- sapply(seq_along(Mins1),function(i) runif(n, Mins1[i],Maxs1[i]))
 colnames(res) <- colnames(x)
 res <- data.table(res)
 }
res
 }
 rnd.data1(dat1,3)
#       Col1      Col2      Col3     Col4      Col5     Col6     Col7      Col8
#1: 21.93711 17.314480  7.351077 28.05296  3.485837 27.97173 29.70568 19.273547
#2: 14.58832  5.026405 36.467826 43.04324 19.002031 29.12006 14.61867  4.809799
#3: 35.23000 17.353508 24.795010 18.65929 19.331303 16.85060 24.13479 49.966598
#       Col9    Col10
#1: 14.90463 40.22131
#2: 16.03714 22.42686
#3: 32.74977 35.68602
 rnd.data1(dat1,3,c(2,5),TRUE)
#       Col2      Col5
#1: 26.87589 22.872162
#2: 19.78380  5.002566
#3: 37.43138 13.187147
 rnd.data1(dat1,3,c(2,5,8),TRUE)
#       Col2     Col5     Col8
#1: 39.63718 19.27199 49.86884
#2: 23.84264 14.19576 42.45117
#3: 41.13644 13.45054 36.48446


A.K.




----- Original Message -----
From: Katherine Bannar-Martin <kbannarm at hotmail.com>
To: r-help at r-project.org
Cc: 
Sent: Friday, October 4, 2013 5:23 PM
Subject: [R] function with loop that goes through columns of dataframes with different dimensions

Writing loops are the bane of my existence. I have this function, which
works:

rnd.data<-function(x){ min.x<-min(x[,2]) max.x<-max(x[,2])
min.y<-min(x[,3]) max.y<-max(x[,3]) data.table(x = runif(34, min.x,
max.x))[, y := runif(34, min.y, max.y)] }

it's purpose is to simulate data within parameters that are dependent on
the column of the dataframe in question for the first data set I wrote it
for had only 2 columns I wanted to simulate samples for however i have
additional dataframes with different numbers of columns ideally i would
write one function with a for loop that could compute samples for all
dataframes I want to input as I need to simulate more than 1000 samples per
dataframe

I tired manipulating the beginning to read as: rnd2.data<-function(x){
n<-dim(x)[2] for(i in 1:n){ if(n > 3){ but then got stuck as to what to do
next

Any help would be greatly appreciated Thanks!

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