Re: [R] help to indexing data frame
On Fri, May 21, 2010 at 11:27 AM, Mohan L l.mohan...@gmail.com wrote: Dear All, I have data some thing like this: sample StateJan Feb Mar A 1 1 1 B1298 12931294 C00 0 D55 5 E 18 18 18 I need to multiply Jan column *1000 and divided by the same number, like this : data[,-(1:2)] * ((data[,2] *1000)/data[ ,2]) data[,-(1:2)] * 1000/data[ ,2] It will work as I expected. I have 0 in the C row. After the calculation it gives NaN zero place. But I need there zero instead of NaN. There may be some solution for this. Thanks Rg Mohan L [[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.
[R] help to indexing data frame
Dear All, I have data some thing like this: sample StateJan Feb Mar A 1 1 1 B1298 12931294 C00 0 D55 5 E 18 18 18 I need to multiply Jan column *1000 and divided by the same number, like this : data[,-(1:2)] * ((data[,2] *1000)/data[ ,2]) I doing some thing wrong with the above to get below result. What I actually need is : StateJan FebMar A 1*1000/1 1*1000/1 1*1000/1 B1298*1000/1298 1293*1000/1298 1294 C00 0 D55 5 E 18 18 18 Here Jan base for me . Any help will be greatly appreciated. Thanks Rg Mohan L [[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.
[R] Help with indexing
Dear R Helpers, I am missing something very elementary here, and I don't seem to get it from the help pages of the ave, seq and seq_along functions, so I wonder if you could offer a quick help. To use an example from an earlier post on this list, I have a dataframe of this kind: dat = data.frame(name = rep(c(Mary, Sam, John), c(3,2,4))) dat$freq = ave(seq_along(dat$name), dat$name, FUN = seq_along) dat name freq 1 Mary1 2 Mary2 3 Mary3 4 Sam1 5 Sam2 6 John1 7 John2 8 John3 9 John4 What I need is another column assigning a number to each name starting from index 100, that is: name freq nb 1 Mary1 100 2 Mary2 100 3 Mary3 100 4 Sam1 101 5 Sam2 101 6 John1 102 7 John2 102 8 John3 102 9 John4 102 What is the easiest way to do this? Thanks a lot for your kind help. Dana __ 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] Help with indexing
try this: # create a factor and then convert back to numeric x$nb - as.integer(factor(x$name, levels=unique(x$name))) + 99 x name freq nb 1 Mary1 100 2 Mary2 100 3 Mary3 100 4 Sam1 101 5 Sam2 101 6 John1 102 7 John2 102 8 John3 102 9 John4 102 On Sat, Nov 21, 2009 at 7:00 PM, Dana Sevak dana.se...@yahoo.com wrote: Dear R Helpers, I am missing something very elementary here, and I don't seem to get it from the help pages of the ave, seq and seq_along functions, so I wonder if you could offer a quick help. To use an example from an earlier post on this list, I have a dataframe of this kind: dat = data.frame(name = rep(c(Mary, Sam, John), c(3,2,4))) dat$freq = ave(seq_along(dat$name), dat$name, FUN = seq_along) dat name freq 1 Mary 1 2 Mary 2 3 Mary 3 4 Sam 1 5 Sam 2 6 John 1 7 John 2 8 John 3 9 John 4 What I need is another column assigning a number to each name starting from index 100, that is: name freq nb 1 Mary 1 100 2 Mary 2 100 3 Mary 3 100 4 Sam 1 101 5 Sam 2 101 6 John 1 102 7 John 2 102 8 John 3 102 9 John 4 102 What is the easiest way to do this? Thanks a lot for your kind help. Dana __ 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 that 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.
Re: [R] Help with indexing
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Dana Sevak Sent: Saturday, November 21, 2009 4:00 PM To: r-help@r-project.org Subject: [R] Help with indexing Dear R Helpers, I am missing something very elementary here, and I don't seem to get it from the help pages of the ave, seq and seq_along functions, so I wonder if you could offer a quick help. To use an example from an earlier post on this list, I have a dataframe of this kind: dat = data.frame(name = rep(c(Mary, Sam, John), c(3,2,4))) dat$freq = ave(seq_along(dat$name), dat$name, FUN = seq_along) dat name freq 1 Mary1 2 Mary2 3 Mary3 4 Sam1 5 Sam2 6 John1 7 John2 8 John3 9 John4 What I need is another column assigning a number to each name starting from index 100, that is: name freq nb 1 Mary1 100 2 Mary2 100 3 Mary3 100 4 Sam1 101 5 Sam2 101 6 John1 102 7 John2 102 8 John3 102 9 John4 102 What is the easiest way to do this? match() will do it: match(names, unique(names)) + 99 [1] 100 100 100 101 101 102 102 102 102 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Thanks a lot for your kind help. Dana __ 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.