[R] For applying formula in rows
Joshua Wiley
jwiley.psych at gmail.com
Tue Jul 12 19:15:17 CEST 2011
Hi Vikas,
Here is one way:
df <- read.table("summary.txt", header = TRUE)
str(df)
df[, "total"] <- rowSums(df[, 3:6])
df[, 3:6] <- apply(df[, 3:6], 2, function(x) x / df[, "total"] * df[,
"new"] * 2)
> head(df)
V1 V2 CaseA CaseC CaseG CaseT new total
1 10 135344109 0 0 24 0 12 1
2 10 135344110 0 24 0 0 12 1
3 10 135344111 0 0 24 0 12 1
4 10 135344112 0 0 24 0 12 1
5 10 135344113 0 0 24 0 12 1
6 10 135344114 24 0 0 0 12 1
Note that I read the data in differently than you did. This matters.
Cheers,
Josh
2011/7/12 Bansal, Vikas <vikas.bansal at kcl.ac.uk>:
> 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.
>
>
> Can you please help me.
>
>
>
>
>
> Thanking you,
> Warm Regards
> Vikas Bansal
> Msc Bioinformatics
> Kings College London
> ______________________________________________
> 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.
>
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/
More information about the R-help
mailing list