我想将一列中的某些值除以一个数字并将该值存储在另一列中。在 R
i want to divide certain values in a column by a number and store that value in another column. in R
this is how my data frame looks
这就是我正在做的事情:
enter code here
nw_dat$cost_of_2_AUD<-if ( nw_dat$Country=="India") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/56.7
} else if (nw_dat$Country=="Phillipines") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/36.97
} else if ( nw_dat$Country=="Brazil") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/4.08
}else if ( nw_dat$Country=="Canada") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/0.94
}else if ( nw_dat$Country=="Indonesia") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/11066.12
}else if ( nw_dat$Country=="New Zealand") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/1.08
}else if ( nw_dat$Country=="Singapore") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/1.03
}else if ( nw_dat$Country=="South Africa") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/10.92
}else if ( nw_dat$Country=="Sri Lanka") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/152.35
}else if ( nw_dat$Country=="Turkey") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/6.57
}else if ( nw_dat$Country=="UAE") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/2.84
}else if ( nw_dat$Country=="United Kingdom") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/0.55
}else if ( nw_dat$Country=="United States") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/0.77
}else {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two
}
但问题是它没有将其转换为正确的值,只有我转换的菲律宾值是正确的,而其他国家/地区的值是错误的。
提前感谢您的帮助。
使用国家名称和相应的值创建一个查找 table 以划分该国家。
#Include all the countries, I am showing only 3 as example.
lookup <- data.frame(Country = c('India', 'Phillipines', 'Brazil'),
value = c(56.7, 36.97, 4.08))
merge
dataframe nw_dat
直接除值.
nw_dat <- transform(merge(nw_dat, lookup, by = 'Country'),
cost_of_2_AUD = Average.Cost.for.two/value)
this is how my data frame looks
这就是我正在做的事情:
enter code here
nw_dat$cost_of_2_AUD<-if ( nw_dat$Country=="India") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/56.7
} else if (nw_dat$Country=="Phillipines") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/36.97
} else if ( nw_dat$Country=="Brazil") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/4.08
}else if ( nw_dat$Country=="Canada") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/0.94
}else if ( nw_dat$Country=="Indonesia") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/11066.12
}else if ( nw_dat$Country=="New Zealand") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/1.08
}else if ( nw_dat$Country=="Singapore") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/1.03
}else if ( nw_dat$Country=="South Africa") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/10.92
}else if ( nw_dat$Country=="Sri Lanka") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/152.35
}else if ( nw_dat$Country=="Turkey") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/6.57
}else if ( nw_dat$Country=="UAE") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/2.84
}else if ( nw_dat$Country=="United Kingdom") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/0.55
}else if ( nw_dat$Country=="United States") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/0.77
}else {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two
}
但问题是它没有将其转换为正确的值,只有我转换的菲律宾值是正确的,而其他国家/地区的值是错误的。
提前感谢您的帮助。
使用国家名称和相应的值创建一个查找 table 以划分该国家。
#Include all the countries, I am showing only 3 as example.
lookup <- data.frame(Country = c('India', 'Phillipines', 'Brazil'),
value = c(56.7, 36.97, 4.08))
merge
dataframe nw_dat
直接除值.
nw_dat <- transform(merge(nw_dat, lookup, by = 'Country'),
cost_of_2_AUD = Average.Cost.for.two/value)