How to insert the values of mathematical expression into a Column in R -


i have dataset following 4 column types, see struct of data frame below

>str(df)  >data.frame':  108517 obs. of  4 variables:  >$ customerid: int  1 2 3 4 5 6 7 8 9 10 ...  >$ activity: chr  " 418 " " 2 " " 1,868 " " 6,319 " ...  >$ country : chr  "germany" "germany" "england" "belgium" ...  >$ customer_group   : int  1 4 3 5 5 5 2 1 1 3 ... 

i want create fifth column, , fill column "total sales" each customer,

(total_sales calculated multiplying customer activity 0.511 , minus discount level based on customer's group)

my code below

>activity <- as.integer(gsub(",", "", df$activity)) >attach(df) >df$total_sales[df$customer_group == 1] <- ((0.511 * activity) - 50) >df$total_sales[df$customer_group == 2] <- ((0.511 * activity) - 65) >df$total_sales[df$customer_group == 3] <- ((0.511 * activity) - 20) >df$total_sales[df$customer_group == 4] <- ((0.511 * activity) - 35) >df$total_sales[df$customer_group == 5] <- ((0.511 * activity) - 0) >detach(df) 
  1. i error attaching dataset

    the following object masked by .globalenv: activity following objects masked df (pos = 3): activity, country, customer_group, customerid

  2. also when continued, following errors "number of items replace not multiple of replacement length"

when manually checked newly created column, total_sales, majority of values not correct (not desired result). doing wrong, please? need help.

thanks.

  1. this not error, warning letting know attach doing. you've specified activity independent vector, column in dataframe you're attaching. when attach dataframe, r has pick between vector you've created , column, because both have same name now.

  2. this error occurs because not comparing vectors of equal length. on left side of these lines, have subset of rows of dataframe. on right side, however, you're transforming entirety of activity vector. these aren't of equal length, r doesn't know them.

if remove attach() , detach() lines , change code rows this, script run fine:

df$total_sales[df$customer_group == 1] <- ((0.511 * as.numeric(gsub(",", "", df$activity)[df$customer_group == 1])) - 50) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -