[R] For applying formula in rows
Berend Hasselman
bhh at xs4all.nl
Wed Jul 13 09:44:40 CEST 2011
Bansal, Vikas wrote:
>
> Dear all,
>
> I have a problem and it is very difficult for me to get a code.
> I am reading a file(attached with this mail) using the code-
>
> df=read.table("summary.txt",fill=T,sep="",colClasses =
> "character",header=T)
> and dataframe df is like this-
>
> V1 V2 CaseA CaseC CaseG CaseT new
> 10 135344109 0 0 1 0 12
> 10 135344110 0 1 0 0 12
> 10 135344111 0 0 1 0 12
> 10 135344112 0 0 1 0 12
> 10 135344113 0 0 1 0 12
> 10 135344114 1 0 0 0 12
> 10 135344115 1 0 0 0 12
> 10 135344116 0 0 0 1 12
> 10 135344117 0 1 0 0 12
> 10 135344118 0 0 0 1 12
>
> I want to apply a formula which is (number/total)*new*2.
> where number is in column caseA,G,C,T and total is sum of these 4
> columns.I will explain with an example.the output of first row should be-
>
> V1 V2 CaseA CaseC CaseG CaseT new
> 10 135344109 0 0 24 0 12
>
> because sum of 3rd,4th,5th and 6th column is 1 for first row.and for case
> A,C and T if we will apply above formula the answer will be zero
> (0/1)*12*2 which is equal to 0 but for Case G-
> (1/1)*12*2 which is equal to 24.
>
Reading the data the same was as Joshua
df <- read.table("summary.txt", header = TRUE)
str(df)
df[, "total"] <- rowSums(df[, 3:6])
and then why not just
df[, 3:6] <- df[, 3:6] / df[, "total"] * df[, "new"] * 2
Berend
--
View this message in context: http://r.789695.n4.nabble.com/For-applying-formula-in-rows-tp3662875p3664410.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list