Hello All,
I need help with my dataframe, it is big but here I am using a small table
as an example.
My dataframe df looks like:
X1 X2 X3
1 2011-02 0.00 96.00
2 2011-02 0.00 2.11
3 2011-02 2.00 3.08
4 2011-02 0.06 2.79
5 2011-02 0.00 96.00
6 2011-02 0.00 97.00
7 2011-02 0.08 2.23
I want values in columns X2 and X3 to be checked if they are greater than
50, if yes, then subtract from '100'
df should look like:
X1 X2 X3
1 2011-02 0.00 4.00
2 2011-02 0.00 2.11
3 2011-02 2.00 3.08
4 2011-02 0.06 2.79
5 2011-02 0.00 4.00
6 2011-02 0.00 3.00
7 2011-02 0.08 2.23
Please help, I will really appreciate that.
Thanks,
Joe
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-substract-a-valur-from-dataframe-with-condition-tp3394907p3394907.html
Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2011-Mar-22 00:03 UTC
[R] How to substract a valur from dataframe with condition
On Mar 21, 2011, at 5:39 PM, joe82 wrote:> Hello All, > > I need help with my dataframe, it is big but here I am using a small > table > as an example. > > My dataframe df looks like: > X1 X2 X3 > 1 2011-02 0.00 96.00 > 2 2011-02 0.00 2.11 > 3 2011-02 2.00 3.08 > 4 2011-02 0.06 2.79 > 5 2011-02 0.00 96.00 > 6 2011-02 0.00 97.00 > 7 2011-02 0.08 2.23 > > I want values in columns X2 and X3 to be checked if they are greater > than > 50, if yes, then subtract from '100' > > df should look like: > > X1 X2 X3 > 1 2011-02 0.00 4.00 > 2 2011-02 0.00 2.11 > 3 2011-02 2.00 3.08 > 4 2011-02 0.06 2.79 > 5 2011-02 0.00 4.00 > 6 2011-02 0.00 3.00 > 7 2011-02 0.08 2.23df[ , 2:3] <- apply(df[ , 2:3], 2, function(x) ifelse(x >50, 100-x, x) ) OR: df[ , 2:3] <- sapply(df[ , 2:3], function(x) ifelse(x >50, 100-x, x) ) (or lapply would work as well) OR; df$X2[df$X2 >50] <- 100 - df$X2[df$X2 >50] df$X1[df$X1 >50] <- 100 - df$X1[df$X1 >50] -- David Winsemius, MD Heritage Laboratories West Hartford, CT
Bill.Venables at csiro.au
2011-Mar-22 01:45 UTC
[R] How to substract a valur from dataframe with condition
dat <- within(dat, {
X2 <- ifelse(X2 > 50, 100-X2, X2)
X3 <- ifelse(X3 > 50, 100-X3, X3)
})
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of joe82
Sent: Tuesday, 22 March 2011 7:40 AM
To: r-help at r-project.org
Subject: [R] How to substract a valur from dataframe with condition
Hello All,
I need help with my dataframe, it is big but here I am using a small table
as an example.
My dataframe df looks like:
X1 X2 X3
1 2011-02 0.00 96.00
2 2011-02 0.00 2.11
3 2011-02 2.00 3.08
4 2011-02 0.06 2.79
5 2011-02 0.00 96.00
6 2011-02 0.00 97.00
7 2011-02 0.08 2.23
I want values in columns X2 and X3 to be checked if they are greater than
50, if yes, then subtract from '100'
df should look like:
X1 X2 X3
1 2011-02 0.00 4.00
2 2011-02 0.00 2.11
3 2011-02 2.00 3.08
4 2011-02 0.06 2.79
5 2011-02 0.00 4.00
6 2011-02 0.00 3.00
7 2011-02 0.08 2.23
Please help, I will really appreciate that.
Thanks,
Joe
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-substract-a-valur-from-dataframe-with-condition-tp3394907p3394907.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at 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.
Peter Ehlers
2011-Mar-22 07:49 UTC
[R] How to substract a valur from dataframe with condition
On 2011-03-21 14:39, joe82 wrote:> Hello All, > > I need help with my dataframe, it is big but here I am using a small table > as an example. > > My dataframe df looks like: > X1 X2 X3 > 1 2011-02 0.00 96.00 > 2 2011-02 0.00 2.11 > 3 2011-02 2.00 3.08 > 4 2011-02 0.06 2.79 > 5 2011-02 0.00 96.00 > 6 2011-02 0.00 97.00 > 7 2011-02 0.08 2.23 > > I want values in columns X2 and X3 to be checked if they are greater than > 50, if yes, then subtract from '100' > > df should look like: > > X1 X2 X3 > 1 2011-02 0.00 4.00 > 2 2011-02 0.00 2.11 > 3 2011-02 2.00 3.08 > 4 2011-02 0.06 2.79 > 5 2011-02 0.00 4.00 > 6 2011-02 0.00 3.00 > 7 2011-02 0.08 2.23 >One more way: d <- transform(d, X1 = pmin(X1, 100 - X1), X2 = pmin(X2, 100 - X2)) [or use within() in place of transform()]. Peter Ehlers