Hello, I'm new in R and I want to do one thing that is very easy in excel, however, I cant do it in R. Suppose we have the data frame: data<- data.frame(A=c("a1","a2","a3","a4","a5")) I need to obtain another column in the same data frame (lets say B=c(b1,b2,b3,b4,b5) in the following way: b1=a1/(a1+a2+a3+a4+a5) b2=a2/(a2+a3+a4+a5) b3=a3/(a3+a4+a5) b4=a4/(a4+a5) b5=a5/a5 a1..a5 and b1...b5 are always numeric values (this is just an example, what I really want is apply this kind of formula to a much larger data frame) I think this is easy for those who are used to work in R (and I suppose there are many different ways), but I can make it in this moment, because I cannot leave behind, the "excel thinking way". I hope you understand my problem and please, help me soon. Alexandre alexbri at netcabo.pt
alexbri wrote:> Hello, I'm new in R and I want to do one thing that is very easy in excel, however, I cant do it in R. > > Suppose we have the data frame: > > > > data<- data.frame(A=c("a1","a2","a3","a4","a5")) > > > > I need to obtain another column in the same data frame (lets say B=c(b1,b2,b3,b4,b5) in the following way: > > > > b1=a1/(a1+a2+a3+a4+a5) > > b2=a2/(a2+a3+a4+a5) > > b3=a3/(a3+a4+a5) > > b4=a4/(a4+a5) > > b5=a5/a5temp <- rev(data$A) data$B <- rev(temp / cumsum(temp)) Uwe Ligges> > > a1..a5 and b1...b5 are always numeric values > > > > (this is just an example, what I really want is apply this kind of formula to a much larger data frame) > > > > I think this is easy for those who are used to work in R (and I suppose there are many different ways), but I can make it in this moment, because I cannot leave behind, the "excel thinking way". > > I hope you understand my problem and please, help me soon. > > > > Alexandre > > > > alexbri at netcabo.pt > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
alexbri at netcabo.pt wrote:> Hello, I'm new in R and I want to do one thing that is very easy in > excel, however, I cant do it in R.Well, if you've deadened your brain by using Excel, no wonder.> Suppose we have the data frame: > > data<- data.frame(A=c("a1","a2","a3","a4","a5"))Oh, for Pete's sake! This makes ``data'' (NOT a good name for an object!) into a data frame with a single column named ``A''. That column will be a factor with 5 entries (an 5 levels) with these levels being (the character strings) "a1","a2","a3","a4", and "a5". Nothing to do with what you actually want.> I need to obtain another column in the same data frame (lets say > B=c(b1,b2,b3,b4,b5) in the following way:This would make B a ***vector*** equal to the concatenation of b1, ..., b5. Perhaps you mean: B <- cbind(b1,b2,b3,b4,b5)> b1=a1/(a1+a2+a3+a4+a5) > > b2=a2/(a2+a3+a4+a5) > > b3=a3/(a3+a4+a5) > > b4=a4/(a4+a5) > > b5=a5/a5 > > a1..a5 and b1...b5 are always numeric values > > (this is just an example, what I really want is apply this kind of > formula to a much larger data frame) >You are very confused. Your notation and your use of the function c() are all wrong. If you are going to use R, get the basic syntax straight. You probably should be using matrices rather than data frames given that the entries are all numeric. Be that as it may, if A is a (numeric) matrix then B <- A/t(apply(A,1,function(x){rev(cumsum(x))})) will give what you appear to want. cheers, Rolf Turner rolf at math.unb.ca