On Sep 5, 2010, at 10:47 AM, rajesh j wrote:
I'm sorry. you seem to have misunderstood my data representation for
input.
Because you did not follow the Posting Guide's advice regarding
producing examples using valid R code. You give me text; I work on text.
#---------
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
#--------
I am going to change the name of the variable to cc because "c" is a
unfortunate name for a variable, since it is also the name of a
crucial function.
Here's what I have
cc<-list(INT=c("1","2","3"),NUM=c("2.34","4.56","6.78"))
I need
c<-list(INT=c(1,2,3),NUM=c(2.34,4.56,6.78))
> cc <- lapply(cc, as.numeric)
> cc
$INT
[1] 1 2 3
$NUM
[1] 2.34 4.56 6.78
--
David.
On Sun, Sep 5, 2010 at 7:57 PM, David Winsemius <dwinsem...@comcast.net
> wrote:
So there is one item per line and the task is to recognize the
strings "INT" and "NUM" and create variables with numeric type and
INT=c(2,3,4)
NUM=c(2.37, 4.56) # ???
I worry that is not a full description of the task .... if there be
more than just two variable names and if all the INTs have the same
name, then they will get overwritten .... but perhaps you have
specified the problem completely, so here goes:
> txt <- textConnection('INT
+ "2"
+ "3"
+ "4"
+
+ NUM
+ "2.37"
+ "4.56"')
> indat <- read.table(txt, stringsAsFactors=FALSE)
> indat$nflag <- as.numeric(indat$V1)
> cumsum(is.na(indat$nflag))
[1] 1 1 1 1 2 2 2
> by(indat$V1, cumsum(is.na(indat$nflag)), function(x)
assign(as.character(x[1]), as.numeric(x[-1]) ,envir = .GlobalEnv) )
cumsum(is.na(indat$nflag)): 1
[1] 1 3 4
-------------------------------------------------------------
cumsum(is.na(indat$nflag)): 2
[1] 2 5
> INT
[1] 1 3 4
> NUM
[1] 2 5
.
On Sep 5, 2010, at 9:33 AM, rajesh j wrote:
The string vector actually comes as a part of a list, and the vector
is named "int", and the numbers are strings. I then have to make it
a vector that is still called "int" and has 4,5,6 etc. the types are
either integer or numeric. The number of items in the vector is
unknown.
here's an example,
a list has vectors
INT
"2"
"3"
"4"
NUM
"2.37"
"4.56"
On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius <dwinsem...@comcast.net
> wrote:
On Sep 5, 2010, at 9:22 AM, rajesh j wrote:
for e.g., I get the following as a string vector
"int" "4" "5" "6"
after reading the first element, I have to convert this to a integer
vector
But what is the right answer? And what number of items are possble
per line? And what are the other possible type identifiers? We need
an example that has enough complexity to allow testing.
--
David.
On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius <dwinsem...@comcast.net
> wrote:
On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
Hi,
Is it possible to convert a string vector to integer or numeric
vector? In
my situation I receive data in a string vector and have to convert
it based
on a given type.
Can you give an example? I don't understand either what sort of
conversion you desire or what you mean by "convert it based on a
given type."
There are a couple of function you may want to consider but I am
having difficulty convincing myself they answer the problem posed:
?charToRaw
?stroi
> strtoi(charToRaw("123 this is a string"), base=16) # convert to
decimal ASCII
[1] 49 50 51 32 116 104 105 115 32 105 115 32 97 32 115 116
114 105 110 103
--
David Winsemius, MD
West Hartford, CT
______________________________________________
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.