Hello together, i have a little problem, to create a new column, in a data.frame. I know i can calculate one column with as a example 2 like this: ORDER$WEIGHT <- ORDER$VALUE * 2 But how can i create the ORDER$WEIGHT with different numbers, like this one. I have a data.frame like this one: ORDER DESTINY VALUE A NY 100 B BER 10 C FRA 100 D WAS 50 I want now the column ORDER$WEIGHT in dependence of "DESTINY". NY should be *3, BER *2, FRA*1 and WAS * 4. The solution look like this one: ORDER DESTINY VALUE WEIGHT A NY 100 300 B BER 10 20 C FRA 100 100 D WAS 50 200 Maybe anyone can help me, how i can do this? Thank you. -- View this message in context: http://r.789695.n4.nabble.com/calculate-an-value-in-dependence-of-another-column-tp4683727.html Sent from the R help mailing list archive at Nabble.com.
Gerrit Eichner
2014-Jan-17 09:36 UTC
[R] calculate an value in dependence of another column
Hi, Mat, e. g., try something like ORDER$VALUE * c( NY = 3, BER = 2, FRA = 1, WAS = 4)[ ORDER$DESTINY] Hth -- Gerrit On Fri, 17 Jan 2014, Mat wrote:> Hello together, > > i have a little problem, to create a new column, in a data.frame. > I know i can calculate one column with as a example 2 like this: > ORDER$WEIGHT <- ORDER$VALUE * 2 > > But how can i create the ORDER$WEIGHT with different numbers, like this one. > > I have a data.frame like this one: > > ORDER DESTINY VALUE > A NY 100 > B BER 10 > C FRA 100 > D WAS 50 > > I want now the column ORDER$WEIGHT in dependence of "DESTINY". NY should be > *3, BER *2, FRA*1 and WAS * 4. > > The solution look like this one: > > ORDER DESTINY VALUE WEIGHT > A NY 100 300 > B BER 10 20 > C FRA 100 100 > D WAS 50 200 > > Maybe anyone can help me, how i can do this? > > Thank you.
Hi,
You could try:
dat1 <- read.table(text="ORDER? DESTINY??? VALUE
A??????? NY????????? 100
B??????? BER????????? 10
C??????? FRA??????? 100
D??????? WAS??????? 50
E??????? BER????????
20",sep="",header=TRUE,stringsAsFactors=FALSE)
?within(dat1,WEIGHT <-
as.numeric(factor(DESTINY,levels=c("FRA","BER","NY","WAS")))*VALUE)
A.K.
On Friday, January 17, 2014 4:15 AM, Mat <matthias.weber at fnt.de> wrote:
Hello together,
i have a little problem, to create a new column, in a data.frame.
I know i can calculate one column with as a example 2 like this:
ORDER$WEIGHT <- ORDER$VALUE * 2
But how can i create the ORDER$WEIGHT with different numbers, like this one.
I have a data.frame like this one:
ORDER? DESTINY? ? VALUE
A? ? ? ? NY? ? ? ? ? 100
B? ? ? ? BER? ? ? ? ? 10
C? ? ? ? FRA? ? ? ? 100
D? ? ? ? WAS? ? ? ? 50
I want now the column ORDER$WEIGHT in dependence of "DESTINY". NY
should be
*3, BER *2, FRA*1 and WAS * 4.
The solution look like this one:
ORDER? DESTINY? ? VALUE? WEIGHT
A? ? ? ? NY? ? ? ? ? 100? ? ? ? ? 300
B? ? ? ? BER? ? ? ? ? 10? ? ? ? ? ? 20
C? ? ? ? FRA? ? ? ? 100? ? ? ? ? ? 100
D? ? ? ? WAS? ? ? ? 50? ? ? ? ? ? 200
Maybe anyone can help me, how i can do this?
Thank you.
--
View this message in context:
http://r.789695.n4.nabble.com/calculate-an-value-in-dependence-of-another-column-tp4683727.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.