Dear all,
for each A == 3 in 'df' I would like to change the variables B and K.
My result should be the whole df and not the subset (A==3)...
df <- data.frame(A = c(1,1,3,2,2,3,3),
B = c(2,1,1,2,7,8,7),
K = c("a.1", "d.2", "f.3",
"a.1", "k.4", "f.9",
"f.5"))
x1 <- within(df[df$A ==3, ], {
B1 <- 5
K1 <- gsub("f","m", K)
})
x2 <- transform(df[df$A==3, ], B1 = 5, K1 = gsub("f","m",
K))
Thanks for any help!
Patrick
Arrg, sorry - of course I don't want *new* variables. So this is my
correct example:
df <- data.frame(A = c(1,1,3,2,2,3,3),
B = c(2,1,1,2,7,8,7),
K = c("a.1", "d.2", "f.3",
"a.1", "k.4", "f.9",
"f.5"))
x1 <- within(df[df$A ==3, ], {
B <- 5
K <- gsub("f","m", K)
})
x2 <- transform(df[df$A==3, ], B = 5, K = gsub("f","m",
K))
Thanks
Patrick
Am 16.01.2011 15:13, schrieb Patrick Hausmann:> Dear all,
>
> for each A == 3 in 'df' I would like to change the variables B and
K.
> My result should be the whole df and not the subset (A==3)...
>
> df <- data.frame(A = c(1,1,3,2,2,3,3),
> B = c(2,1,1,2,7,8,7),
> K = c("a.1", "d.2", "f.3",
> "a.1", "k.4", "f.9", "f.5"))
>
> x1 <- within(df[df$A ==3, ], {
> B1 <- 5
> K1 <- gsub("f","m", K)
> })
>
> x2 <- transform(df[df$A==3, ], B1 = 5, K1 =
gsub("f","m", K))
>
> Thanks for any help!
> Patrick
>
> ______________________________________________
> 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.
On 16/01/2011 9:13 AM, Patrick Hausmann wrote:
> Dear all,
>
> for each A == 3 in 'df' I would like to change the variables B and
K.
> My result should be the whole df and not the subset (A==3)...
>
> df<- data.frame(A = c(1,1,3,2,2,3,3),
> B = c(2,1,1,2,7,8,7),
> K = c("a.1", "d.2",
"f.3",
> "a.1", "k.4",
"f.9", "f.5"))
>
> x1<- within(df[df$A ==3, ], {
This is the problem: you've subsetted the dataset here, and lost all
the other lines. You want something like this:
x3 <- within(df, {
B <- ifelse(A == 3, 5, B)
K <- ifelse(A == 3, gsub("f", "m", K),
as.character(K)) }
)
You need the "as.character" as by default K will be a factor, not a
character vector, and ifelse will take the factor level instead of the
label.
If the condition was more complicated than A == 3, you might want to
calculate it first and use the calculated value each time:
x3 <- within(df, {
changes <- A == 3
B <- ifelse(changes, 5, B)
K <- ifelse(changes, gsub("f", "m", K),
as.character(K))
rm(changes)
}
)
> B1<- 5
> K1<- gsub("f","m", K)
> })
>
> x2<- transform(df[df$A==3, ], B1 = 5, K1 =
gsub("f","m", K))
>
> Thanks for any help!
> Patrick
>
> ______________________________________________
> 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.
Hi
Try the following...
df <- data.frame(A = c(1,1,3,2,2,3,3),
B = c(2,1,1,2,7,8,7),
K = c("a.1", "d.2", "f.3",
"a.1", "k.4", "f.9", "f.5"))
df$ID<-rownames(df)
df$K<-as.character(as.character(df$K))
changefunction<-function(z)
{
tmp <- lapply(split(z, z[,4]),
function(x) within(x, if(A==3)B <- 5 ))
dat2<-tmp
df<-unsplit(dat2,df$ID)
tmp <- lapply(split(df, df[,4]),
function(x) within(x, if(A==3)K <- chartr("f","m",K)))
dat2<-tmp
df<-unsplit(dat2,df$ID)
return(df)
}
dfnew<-changefunction(df)
df$ID<-NULL
dfnew$ID<-NULL
dfnew
Regards
Vijayan Padmanabhan
"What is expressed without proof can be denied without proof" -
Euclide.
Can you avoid printing this?
Think of the environment before printing the email.
-------------------------------------------------------------------------------
Please visit us at www.itcportal.com
******************************************************************************
This Communication is for the exclusive use of the intended recipient (s) and
shall
not attach any liability on the originator or ITC Ltd./its Subsidiaries/its
Group
Companies. If you are the addressee, the contents of this email are intended for
your
use only and it shall not be forwarded to any third party, without first
obtaining
written authorisation from the originator or ITC Ltd./its Subsidiaries/its Group
Companies. It may contain information which is confidential and legally
privileged
and the same shall not be used or dealt with by any third party in any manner
whatsoever without the specific consent of ITC Ltd./its Subsidiaries/its Group
Companies.
[[alternative HTML version deleted]]