[R] R Function to extract columnNames

arun smartpink111 at yahoo.com
Thu May 2 04:12:00 CEST 2013


Hi,
If you are using Rstudio, please check this link (http://support.rstudio.org/help/discussions/problems/850-ddply-misbehaving-in-rstudio-and-only-in-rstudio).
A.K.





----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc: David Winsemius <dwinsemius at comcast.net>
Sent: Wednesday, May 1, 2013 9:49 PM
Subject: Re: R Function to extract columnNames

HI,

I am not sure what is wrong in your side.  I cut and paste the same code and I get this:

library(plyr)
set.seed(25)
 mydf<- data.frame(subid=rep(1:5,each=3),Col=sample(c(0:5,NA),15,replace=TRUE))
 retsample <- function(df, Column,size) {
 set.seed(1234)
 mysel <- ddply(df, .(subid),function(x) summarize(x,missing=sum(is.na(x[[Column]])|x[[Column]]==0)))
 myids<- mysel[mysel$missing==0,1]
 sample_regular<- subset(df,subid%in% sample(myids,size,replace=TRUE))    
 return(sample_regular)
   }
 retsample(mydf,"Col",5) 

 # subid Col
#1      1   2
#2      1   4
#3      1   1
#10     4   1
#11     4   2
#12     4   2


 sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8    
 [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] plyr_1.8        lattice_0.20-15 chron_2.3-43    stringr_0.6.2  
[5] reshape2_1.2.2 


A.K.



>David, 
>I cut-n-paste and tried to run and I am getting the follwoing error: 
>BTW, I really, really appreciate your help. 
>
>> library(plyr) 
>> set.seed(25) 
>> mydf<- data.frame(subid=rep(1:5,each=3),Col=sample(c(0:5,NA),15,replace=TRUE)) 
>> retsample <- function(df, Column,size) { 
>+ set.seed(1234) 
>+ mysel <- ddply(df, .(subid),function(x) summarize(x,missing=sum(is.na(x[[Column]])|x[[Column]]==0))) 
>+ myids<- mysel[mysel$missing==0,1] 
>+ sample_regular<- subset(df,subid%in% sample(myids,size,replace=TRUE))     
>+ return(sample_regular) 
>+   } 
>> retsample(mydf,"Col",5) 
>Error in is.list(by) : 'by' is missing


----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc: David Winsemius <dwinsemius at comcast.net>
Sent: Wednesday, May 1, 2013 4:01 PM
Subject: Re: R Function to extract columnNames

Hi ST,

You could try this:
library(plyr)
set.seed(25)
mydf<- data.frame(subid=rep(1:5,each=3),Col=sample(c(0:5,NA),15,replace=TRUE))
retsample <- function(df, Column,size) {
set.seed(1234)
mysel <- ddply(df, .(subid),function(x) summarize(x,missing=sum(is.na(x[[Column]])|x[[Column]]==0)))
myids<- mysel[mysel$missing==0,1]
sample_regular<- subset(df,subid%in% sample(myids,size,replace=TRUE))    
return(sample_regular)
  }
retsample(mydf,"Col",5)
#   subid Col
#1      1   2
#2      1   4
#3      1   1
#10     4   1
#11     4   2
#12     4   2


A.K.



>Thanks David for the tip. I dont think I still have this working for a 
function that i am writing. Say col, subid are column names in my 
dataframe >mydf. My calling function is retsample(mydf,"Col",5) 
>This below function fails in ddply call. In this call I am counting 
how many nulls or zeros exist for that colum that I am passing. I get 
"Error in >eval(expr, envir, enclos) : object 'myCol' not found" 
>
>#Get appropriate random sample to print for variable col from dataframe df, that has 0 missing values 
>retsample <- function(df,x,size ) { 
 >     set.seed(1234) 
  >    myCol <- df[[x]] 
  >    mysel <- ddply(df,c("subid"),summarise,missing=sum(is.na(myCol)| myCol==0)) 
  >    #select subjects with no missing values 
  >    myids <- mysel[mysel$missing==0,1] 
  >    sample_regular <- subset(df,subid %in% sample(myids, size=size)) 
  >    return(sample_regular) 
>} 
>
>Thank You for your assistance. 
>-ST 




----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Tuesday, April 30, 2013 9:00 AM
Subject: Re: R Function to extract columnNames

Hi,
May be this helps:
funcName<- function(df1, x){
 whatCol=df1[[x]]
 print("Got it")
 print(whatCol)
 }
 
funcName(df,"ColA")
#[1] "Got it"
#[1] 1 2 3 4 5
  funcName(df,"ColB")
#[1] "Got it"
#[1] A B C D E
#Levels: A B C D E


A.K.


>I am trying to extract the 2nd column from a dataframe using a function 
called funcName. Note this is an example that I need cos I am using it 
to >read the values I pass into a function call - these passed values 
represent dataframe column names. I am trying to use this concept for a 
vary large >dataframe with 100+ columns. 
>
>ColA <- c(1,2,3,4,5) 
>ColB <- c("A","B","C","D","E") 
>df <- data.frame(ColA,ColB) 
>
>funcName <- function(x) { 
> whatCol = paste("df",x,sep="$") 
 >print("Got it",whatCol) 
>} 
>
>funcName("ColA") 
>
>Please advise, since this code is not working. Thanks in advance. 
>
>-ST




More information about the R-help mailing list