Re: [R] changing colnames
Hi, You could try: dat1- read.table(text= X1,X2,X3 age,race,stat 12,2,35 17,6,55 ,sep=,,header=TRUE,stringsAsFactors=FALSE) colnames(dat1)- dat1[1,] dat1- dat1[-1,] dat1[]-lapply(dat1,as.numeric) row.names(dat1)- 1:nrow(dat1) dat1 # age race stat #1 12 2 35 #2 17 6 55 A.K. From: farnoosh sheikhi farnoosh...@yahoo.com To: smartpink...@yahoo.com smartpink...@yahoo.com Sent: Wednesday, August 14, 2013 2:45 PM Subject: changing colnames Hi Arun, I have a data set as follow: X1 X2 X3 age race stat 12235 17655 The column names are currently X1 X2 X3, but I want to drop them and have age, race and stat as column names, like below: age race stat 12235 17655 Do you know how to do that? Thanks a lot. __ 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] changing colnames
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of arun Sent: Wednesday, August 14, 2013 11:56 AM To: farnoosh sheikhi Cc: R help Subject: Re: [R] changing colnames Hi, You could try: dat1- read.table(text= X1,X2,X3 age,race,stat 12,2,35 17,6,55 ,sep=,,header=TRUE,stringsAsFactors=FALSE) colnames(dat1)- dat1[1,] dat1- dat1[-1,] dat1[]-lapply(dat1,as.numeric) row.names(dat1)- 1:nrow(dat1) dat1 # age race stat #1 122 35 #2 176 55 A.K. Well, your data does not match up with what arun used. You have space delimited variable names and no spaces between any of the numbers. If the number should be broken up like arun has comma separated, then it is even easier. I stored the following data in a file, temp.txt, X1 X2 X3 age race stat 12 2 35 17 6 55 and read it like this dat1- read.table(c:/tmp/temp.txt, header=TRUE, skip=1) and got this dat1 age race stat 1 122 35 2 176 55 Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA From: farnoosh sheikhi farnoosh...@yahoo.com To: smartpink...@yahoo.com smartpink...@yahoo.com Sent: Wednesday, August 14, 2013 2:45 PM Subject: changing colnames Hi Arun, I have a data set as follow: X1 X2 X3 age race stat 12235 17655 The column names are currently X1 X2 X3, but I want to drop them and have age, race and stat as column names, like below: age race stat 12235 17655 Do you know how to do that? Thanks a lot. __ 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.
Re: [R] changing colnames
Thanks for the code. It was so simple and worked perfectly. I really appreciate it. Best,Farnoosh Sheikhi Cc: R help r-help@r-project.org Sent: Wednesday, August 14, 2013 11:56 AM Subject: Re: changing colnames Hi, You could try: dat1- read.table(text= X1,X2,X3 age,race,stat 12,2,35 17,6,55 ,sep=,,header=TRUE,stringsAsFactors=FALSE) colnames(dat1)- dat1[1,] dat1- dat1[-1,] dat1[]-lapply(dat1,as.numeric) row.names(dat1)- 1:nrow(dat1) dat1 # age race stat #1 12 2 35 #2 17 6 55 A.K. Sent: Wednesday, August 14, 2013 2:45 PM Subject: changing colnames Hi Arun, I have a data set as follow: X1 X2 X3 age race stat 12235 17655 The column names are currently X1 X2 X3, but I want to drop them and have age, race and stat as column names, like below: age race stat 12235 17655 Do you know how to do that? Thanks a lot. [[alternative HTML version deleted]] __ 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] changing colnames in dataframes
thank you all, Jagat, Bert and Prof. Ripley for your kind help. The PROBLEM was: give descripitve names to the columns in JJ-data.frame( c( as.character(rep( gender,3))) , c( F76,6- F83, F90) ) Possible SOLUTIONs are: 1.JJ - data.frame( gender=c( as.character(rep( gender,3))) , J.value=c(F76,6- F83, F90) ) 2. names(JJ) - c(gender,J.value) Now I can continue my work being happy with R. hth Wolfgang __ 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] changing colnames in dataframes
dear all, I'm building new dataframes from bigger one's using e.g. columns F76, F83, F90: JJ-data.frame( c( as.character(rep( gender,3))) , c( F76,6- F83, F90) ) Looking into JJ one has: c.as.character.rep.gender..8... c.6...F73..F78..F79..F82..6...F84..F94..F106..F109 1 w 2 2 w 3 3 m 1 etc. Instead of the automatic colnames 'c.as.character.rep.gender..8' I like to have the colnames 'gender' and 'J.value'. I tried levels, labels etc but without success. OK, I can fix(FF) manually, but this is bad, because it has to be done again and again .. OK, I can do gender.J-c( as.character(rep( gender,3))) Jvalue-c( F76,6- F83, F90) JJ-data.frame(gender.J, J.value) but this is not optimal because 'gender' is depending on rep(.., n) and I have many dataframes KK, LL, .. using 'gender' with different size. Question: is it possible to do something like JJ-data.frame(c( as.character(rep( gender,3))),c( F76,6- F83, F90), ?? col.names=c(gender,Jvalue) ?? ) Sorry, if my question is elementary, but I could not find a fix. Thank your for your advice. hth Wolfgang __ 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] changing colnames in dataframes
See the help page. You name the arguments, e.g. JJ - data.frame( gender= c( as.character(rep( gender,3))) , J.value -c( F76 ,6-F83, F90) ) The help says Arguments: ...: these arguments are of either the form 'value' or 'tag = value'. Component names are created based on the tag (if present) or the deparsed argument itself. On Wed, 3 Dec 2008, Wolfgang Lindner wrote: dear all, I'm building new dataframes from bigger one's using e.g. columns F76, F83, F90: JJ-data.frame( c( as.character(rep( gender,3))) , c( F76,6- F83, F90) ) Looking into JJ one has: c.as.character.rep.gender..8... c.6...F73..F78..F79..F82..6...F84..F94..F106..F109 1 w 2 2 w 3 3 m 1 etc. Instead of the automatic colnames 'c.as.character.rep.gender..8' I like to have the colnames 'gender' and 'J.value'. I tried levels, labels etc but without success. OK, I can fix(FF) manually, but this is bad, because it has to be done again and again .. OK, I can do gender.J-c( as.character(rep( gender,3))) Jvalue-c( F76,6- F83, F90) JJ-data.frame(gender.J, J.value) but this is not optimal because 'gender' is depending on rep(.., n) and I have many dataframes KK, LL, .. using 'gender' with different size. Question: is it possible to do something like JJ-data.frame(c( as.character(rep( gender,3))),c( F76,6- F83, F90), ?? col.names=c(gender,Jvalue) ?? ) Sorry, if my question is elementary, but I could not find a fix. Thank your for your advice. hth Wolfgang -- 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.