GUO Dong 郭东
2010-Apr-24 13:13 UTC
[R] assign value between different type: Double vs Integer
Dear list, just to put it in a simple way: i read.csv from csv file to create a gdata then, create array gdata34 however, when making a loop for assigning gdata34[1,m]<-gdata[m,4], this is what happen gdata[1,4] 's real value is 10354, however, the gdata34[1,4] turns to be 883 then i checked the type: gdata[1,4] is integer, while gdata34[1,4] is double. Can any one give me some help on it???? Many thanks in advance. Best regards, Dong [[alternative HTML version deleted]]
Dear list, just to put it in a simple way: i read.csv from csv file to create a gdata then, create array gdata34 however, when making a loop for assigning gdata34[1,m]<-gdata[m,4], this is what happen gdata[1,4] 's real value is 10354, however, the gdata34[1,4] turns to be 883 then i checked the type: gdata[1,4] is integer, while gdata34[1,4] is double. Can any one give me some help on it???? Many thanks in advance. Best regards, Dong
Dong GUO
2010-Apr-24 14:51 UTC
[R] Fwd: assign value between different type: Double vs Integer
---------- Forwarded message ---------- From: Dong GUO <cguous@gmail.com> Date: Sat, Apr 24, 2010 at 4:50 PM Subject: Re: [R] assign value between different type: Double vs Integer To: Sarah Goslee <sarah.goslee@gmail.com> Thanks, Sarah. the loop is all right. this is what I just experimented. and when trying to do as.numeric (ggg), and I got the 883 coming out, which basically is the point here. somehow in the process, it transformed the 10354 to 883 as a different a type from Double to Numeric... Dont know how this can happen..> gdata[1,5][1] 10354 57290 Levels: -1234 -294 -5 -534 ... 0 1 10 100 1000 10000 100000 ... 9999> typeof(gdata[1,5])[1] "integer"> ggg<-gdata[1,5] > ggg[1] 10354 57290 Levels: -1234 -294 -5 -534 ... 0 1 10 100 1000 10000 100000 ... 9999> integer(ggg)[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [36] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [71] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [596] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [631] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [666] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [701] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [736] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [771] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [806] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [841] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [876] 0 0 0 0 0 0 0 0> ggg[1][1] 10354 57290 Levels: -1234 -294 -5 -534 ... 0 1 10 100 1000 10000 100000 ... 9999> as.double(ggg)[1] 883> as.numeric(ggg)[1] 883> as.integer(ggg)[1] 883> typeof(ggg)[1] "integer"> ggg[1] 10354 57290 Levels: -1234 -294 -5 -534 ... 0 1 10 100 1000 10000 100000 ... 9999> as.numeric(ggg)[1] 883> ggg[0]factor(0) 57290 Levels: -1234 -294 -5 -534 ... 0 1 10 100 1000 10000 100000 ... 9999> factor(ggg)[1] 10354 Levels: 10354> aaa<-factor(ggg) > aaa[1] 10354 Levels: 10354> typeof(aaa)[1] "integer"> aaa<-as.numeric(factor(ggg)) > aaa[1] 1> aaa[1] 1>On Sat, Apr 24, 2010 at 4:45 PM, Sarah Goslee <sarah.goslee@gmail.com> wrote:> On Sat, Apr 24, 2010 at 9:31 AM, Dong GUO <cguous@gmail.com> wrote: >> Dear list, >> >> just to put it in a simple way: >> >> i read.csv from csv file to create a gdata >> >> then, create array gdata34 >> >> however, when making a loop for assigning gdata34[1,m]<-gdata[m,4], >> this is what happen > > So you assigned (m == 4) > > gdata34[1, 4] <- gdata[4, 4] > > >> gdata[1,4] 's real value is 10354, however, the gdata34[1,4] turns to be883> > But now you expect > gdata[1,4] == gdata34[1,4] > > If your loop description is correct, > gdata34[1,4] <- gdata[4, 4] > and > gdata34[1,1] <- gdata[1,4] > > Something is wrong either in your description or > your check. I don't think it has anything to do with > storage mode. > > Sarah > >> then i checked the type: gdata[1,4] is integer, while gdata34[1,4] isdouble.>> >> Can any one give me some help on it???? >> >> Many thanks in advance. >> >> Best regards, >> Dong >> > -- > Sarah Goslee > http://www.functionaldiversity.org >[[alternative HTML version deleted]]
Sarah Goslee
2010-Apr-24 15:02 UTC
[R] assign value between different type: Double vs Integer
Ah, you didn't provide a complete description of your problem. gdata is a factor. You need to read about converting factors to numeric, or perhaps reconsider your method of creating or importing gdata. as.numeric(somefactor) does not extract the printed value, it extracts the *order of the factor*.> somefactor <- as.factor(c(1,2,6,4)) > somefactor[1] 1 2 6 4 Levels: 1 2 4 6> as.numeric(somefactor)[1] 1 2 4 3> as.numeric(as.character(somefactor))[1] 1 2 6 4> > > somefactor <- as.factor(c("a", "b", "d", "c")) > somefactor[1] a b d c Levels: a b c d> as.numeric(somefactor)[1] 1 2 4 3 Please read the posting guide, and follow the instructions therein for providing a clear and complete question. Plus, I think this is a FAQ. Sarah On Sat, Apr 24, 2010 at 10:50 AM, Dong GUO <cguous at gmail.com> wrote:> Thanks, Sarah. > the loop is all right. this is what I just experimented. > and when trying to do as.numeric (ggg), and I got the 883 coming out, which > basically is the point here. somehow in the process, it transformed the > 10354 to 883 as a different a type from Double to Numeric... > Dont know how this can happen.. >-- Sarah Goslee http://www.functionaldiversity.org
jim holtman
2010-Apr-25 04:04 UTC
[R] assign value between different type: Double vs Integer
If you are looping doing the assignment: gdata34[1,m]<-gdata[m,4] why would yo expect that gdata[1,4] would have the same value as gdata[1,4]. Do you have your indices reversed? You need to provide at least the data in 'gdata' so we have a reproducible example. On Sat, Apr 24, 2010 at 9:13 AM, GUO Dong 郭东 <guo.dong99@gmail.com> wrote:> Dear list, > > just to put it in a simple way: > > i read.csv from csv file to create a gdata > > then, create array gdata34 > > however, when making a loop for assigning gdata34[1,m]<-gdata[m,4], this is > what happen > > gdata[1,4] 's real value is 10354, however, the gdata34[1,4] turns to be > 883 > > then i checked the type: gdata[1,4] is integer, while gdata34[1,4] is > double. > > Can any one give me some help on it???? > > Many thanks in advance. > > Best regards, > Dong > > > > [[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<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 that you are trying to solve? [[alternative HTML version deleted]]