Re: [R] R wildcards, sapply and as.factor
HI, Try this: df1<-read.table(text=" namestub1 namestub2 name1 name2 A1 B1 A11 B11 A1 B2 A11 B22 A2 B1 A22 B11 A2 B2 A22 B22 A3 B1 A33 B11 A3 B2 A33 B22 ",sep="",header=TRUE,stringsAsFactors=FALSE) str(df1) #'data.frame': 6 obs. of 4 variables: # $ namestub1: chr "A1" "A1" "A2" "A2" ... # $ namestub2: chr "B1" "B2" "B1" "B2" ... # $ name1 : chr "A11" "A11" "A22" "A22" ... # $ name2 : chr "B11" "B22" "B11" "B22" ... nm<-names(df1)[grepl("^namestub",names(df1))] df1[nm]<-lapply(df1[nm],as.factor) str(df1) #'data.frame': 6 obs. of 4 variables: # $ namestub1: Factor w/ 3 levels "A1","A2","A3": 1 1 2 2 3 3 # $ namestub2: Factor w/ 2 levels "B1","B2": 1 2 1 2 1 2 # $ name1 : chr "A11" "A11" "A22" "A22" ... # $ name2 : chr "B11" "B22" "B11" "B22" ... A.K. - Original Message - From: Fg Nu To: "r-help@r-project.org" Cc: Sent: Wednesday, November 14, 2012 6:50 AM Subject: [R] R wildcards, sapply and as.factor I want to change the type to factor of all variables in a data frame whose names match a certain pattern. So here I am trying to change the type to factor of all variables whose name begins with namestub in the dataframe df. attach(df)sapply(grep(glob2rx("namestub*"),names(df)),as.factor) But this doesn't work since >levels(df$namestub1)NULL __ R-help@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. __ R-help@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.
[R] R wildcards, sapply and as.factor
I want to change the type to factor of all variables in a data frame whose names match a certain pattern. So here I am trying to change the type to factor of all variables whose name begins with namestub in the dataframe df. attach(df)sapply(grep(glob2rx("namestub*"),names(df)),as.factor) But this doesn't work since >levels(df$namestub1)NULL __ R-help@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.
Re: [R] Wildcards
On Mon, 15 Oct 2007, jim holtman wrote: > for (i in list.files(pattern=".*\\.R$")) source(i) Using Sys.glob() is perhaps more natural for most users: for(file in Sys.glob("*.R")) source(file) It is referenced from ?list.files these days. > On 10/15/07, subura <[EMAIL PROTECTED]> wrote: >> >> Care to explain how i can use a wildcard expression to "source" all files >> ending with .R in a subdirectory ? I've tried something like this >> 'source(glob2rx("*.R"))' without success. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@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.
Re: [R] Wildcards
subura said the following on 10/15/2007 12:04 PM: > Care to explain how i can use a wildcard expression to "source" all files > ending with .R in a subdirectory ? I've tried something like this > 'source(glob2rx("*.R"))' without success. > > Thank you Try R.files <- list.files(my.path, pattern = glob2rx("*.R"), full = TRUE) invisible(sapply(R.files, source)) HTH, --sundar __ R-help@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.
Re: [R] Wildcards
for (i in list.files(pattern=".*\\.R$")) source(i) On 10/15/07, subura <[EMAIL PROTECTED]> wrote: > > Care to explain how i can use a wildcard expression to "source" all files > ending with .R in a subdirectory ? I've tried something like this > 'source(glob2rx("*.R"))' without success. > > Thank you > -- > View this message in context: > http://www.nabble.com/Wildcards-tf4627981.html#a13214214 > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@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.
[R] Wildcards
Care to explain how i can use a wildcard expression to "source" all files ending with .R in a subdirectory ? I've tried something like this 'source(glob2rx("*.R"))' without success. Thank you -- View this message in context: http://www.nabble.com/Wildcards-tf4627981.html#a13214214 Sent from the R help mailing list archive at Nabble.com. __ R-help@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.