I'm sure I'm missing something obvious here, but I can't find the solution (including in the FAQ etc.). I have a vector of names of variables like this: NRes.x.y. where x and y are numbers. I want to extract these numbers as numbers to use elsewhere. I can extract the numbers as a list of characters using strsplit(), and convert that to a data frame, e.g.: NAMES=c("NRes.1.2.", "NRes.1.3.", "NRes.1.4.", "NRes.1.5.", "NRes.1.6.") NUMBERS=strsplit(gsub("NRes.","", NAMES, perl =T), '.', fixed = TRUE) NUMBERS.df=t(data.frame(NUMBERS)) But I now want to convert the characters to be numeric. Using as.numeric(NUMBERS.df) converts them, but to a vector. How can I convert and keep as a data frame? I could use this: matrix(as.numeric(NUMBERS.df), ncol=dim(NUMBERS.df)[2]) but I seem to be jumping through far too many hoops: there must be an easier way. An suggestions? Bob -- Bob O'Hara Department of Mathematics and Statistics P.O. Box 68 (Gustaf H??llstr??min katu 2b) FIN-00014 University of Helsinki Finland Telephone: +358-9-191 51479 Mobile: +358 50 599 0540 Fax: +358-9-191 51400 WWW: http://www.RNI.Helsinki.FI/~boh/ Journal of Negative Results - EEB: www.jnr-eeb.org
Sundar Dorai-Raj
2005-Aug-25 12:08 UTC
[R] Converting characters to numbers in data frames
Anon. wrote:> I'm sure I'm missing something obvious here, but I can't find the > solution (including in the FAQ etc.). > > I have a vector of names of variables like this: NRes.x.y. where x and y > are numbers. I want to extract these numbers as numbers to use > elsewhere. I can extract the numbers as a list of characters using > strsplit(), and convert that to a data frame, e.g.: > > NAMES=c("NRes.1.2.", "NRes.1.3.", "NRes.1.4.", "NRes.1.5.", "NRes.1.6.") > NUMBERS=strsplit(gsub("NRes.","", NAMES, perl =T), '.', fixed = TRUE) > NUMBERS.df=t(data.frame(NUMBERS)) > > But I now want to convert the characters to be numeric. Using > as.numeric(NUMBERS.df) converts them, but to a vector. How can I > convert and keep as a data frame? I could use this: > > matrix(as.numeric(NUMBERS.df), ncol=dim(NUMBERS.df)[2]) > > but I seem to be jumping through far too many hoops: there must be an > easier way. An suggestions? > > Bob >How about this? NUMBERS <- lapply(strsplit(NAMES, "\\."), "[", -1) as.data.frame(do.call("rbind", lapply(NUMBERS, as.numeric))) --sundar
Try: NAMES=c("NRes.1.2.", "NRes.1.3.", "NRes.1.4.", "NRes.1.5.", "NRes.1.6.") pattern<-"NRes\.([0-9]*)\.([0-9]*)\." data.frame(x=sub(pattern,"\\1",NAMES),y=sub(pattern,"\\2",NAMES)) @ output-start Thu Aug 25 14:08:50 2005 x y 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 output-end Peter Wolf Anon. wrote:>I'm sure I'm missing something obvious here, but I can't find the >solution (including in the FAQ etc.). > >I have a vector of names of variables like this: NRes.x.y. where x and y >are numbers. I want to extract these numbers as numbers to use >elsewhere. I can extract the numbers as a list of characters using >strsplit(), and convert that to a data frame, e.g.: > >NAMES=c("NRes.1.2.", "NRes.1.3.", "NRes.1.4.", "NRes.1.5.", "NRes.1.6.") >NUMBERS=strsplit(gsub("NRes.","", NAMES, perl =T), '.', fixed = TRUE) >NUMBERS.df=t(data.frame(NUMBERS)) > >But I now want to convert the characters to be numeric. Using >as.numeric(NUMBERS.df) converts them, but to a vector. How can I >convert and keep as a data frame? I could use this: > >matrix(as.numeric(NUMBERS.df), ncol=dim(NUMBERS.df)[2]) > >but I seem to be jumping through far too many hoops: there must be an >easier way. An suggestions? > >Bob > >
Gabor Grothendieck
2005-Aug-25 12:36 UTC
[R] Converting characters to numbers in data frames
On 8/25/05, Anon. <bob.ohara at helsinki.fi> wrote:> I'm sure I'm missing something obvious here, but I can't find the > solution (including in the FAQ etc.). > > I have a vector of names of variables like this: NRes.x.y. where x and y > are numbers. I want to extract these numbers as numbers to use > elsewhere. I can extract the numbers as a list of characters using > strsplit(), and convert that to a data frame, e.g.: > > NAMES=c("NRes.1.2.", "NRes.1.3.", "NRes.1.4.", "NRes.1.5.", "NRes.1.6.") > NUMBERS=strsplit(gsub("NRes.","", NAMES, perl =T), '.', fixed = TRUE) > NUMBERS.df=t(data.frame(NUMBERS)) > > But I now want to convert the characters to be numeric. Using > as.numeric(NUMBERS.df) converts them, but to a vector. How can I > convert and keep as a data frame? I could use this: > > matrix(as.numeric(NUMBERS.df), ncol=dim(NUMBERS.df)[2]) > > but I seem to be jumping through far too many hoops: there must be an > easier way. An suggestions?Try this: read.table(textConnection(NAMES), sep = ".")[,2:3] You may also want to add the col.names= argument to the read.table call if you want prettier column names. Also I think Names and Numbers would be sufficient to distinguish them from potential lower case counterparts.