0

Hi I have a simple function:

same_picking <- function(cena){
  data_model2$price_model2 <- 0.6 + cena * data_model2$item_SKU + 0.4
}

I would like the output to be rewritten in a column of a data.frame.

currently, because I still did not get the first writing of a function the column is still filled with NAs.. but I would like that after every run of a function the values would be rewriten in theat column.

count_code  sifra   item_SKU    price_model2
281         0421    2           NA
683         0499    5           NA
903         0654    3           NA
7390        0942    3           NA
2778        0796    5           NA
2778        0796    7           NA
7066        0907    83          NA
281         0421    2           NA

I have tried with the comands: data.frame and within... but it got me nowhere.

I would appraciate the help. Andraz

Solution:

same_picking <- function(cena){
      data_model2$price_model2 <<- 0.6 + cena * data_model2$item_SKU + 0.4
}

<<- operator allows you to access the object from the ouside. Very clean :)

5
  • 1
    The comma will not work data_model1, data_model2$price_model2. Do you have two datasets? Perhaps you need data_model2$price_model2 <- 0.6 + cena * data_model2$item_SKU + 0.4; data_model2 Also it is better to use [ instead of $ Commented Oct 11, 2016 at 7:36
  • typo... only one dataset Commented Oct 11, 2016 at 7:39
  • You need the return statement for data_model2 Commented Oct 11, 2016 at 7:40
  • isn't the return statement already specified? "data_model2$price_model2 <- 0.6 + cena * data_model2$item_SKU + 0.4" Commented Oct 11, 2016 at 7:53
  • No, you are only creating a column. You have to return the dataset object Commented Oct 11, 2016 at 8:04

1 Answer 1

1

The simplest way would be to return the df from function:

df <- read.table(
  text = "count_code  sifra   item_SKU    price_model2
          281         0421    2           NA
          683         0499    5           NA
          903         0654    3           NA
          7390        0942    3           NA
          2778        0796    5           NA
          2778        0796    7           NA
          7066        0907    83          NA
          281         0421    2           NA",
  header = TRUE)

head(df, 2)
#   count_code sifra item_SKU price_model2
# 1        281   421        2           NA
# 2        683   499        5           NA

# 1st ---------------------------------------------------------------------

same_picking_1 <- function(df, cena){
  df$price_model2 <- 0.6 + cena * df$item_SKU + 0.4
  return(df)
}

df2 <- same_picking_1(df, 1)

head(df2, 2)
#   count_code sifra item_SKU price_model2
# 1        281   421        2            3
# 2        683   499        5            6

Other options, data.table and dplyr:

same_picking_2 <- function(cena, item_SKU){
  0.6 + cena * df$item_SKU + 0.4
}

# data.table --------------------------------------------------------------

library(data.table)
dt <- data.table(df)
dt[, price_model2 := same_picking_2(1, item_SKU)]

head(dt, 2)
#    count_code sifra item_SKU price_model2
# 1:        281   421        2            3
# 2:        683   499        5            6

# dplyr -------------------------------------------------------------------

library(dplyr)
df3 <- df %>% mutate(price_model2 = same_picking_2(1, item_SKU))

head(df3, 2)
#   count_code sifra item_SKU price_model2
# 1        281   421        2            3
# 2        683   499        5            6

Edit after OP comment:

You can also wrap data.table solution into a function

# data.table --------------------------------------------------------------

library(data.table)

same_picking_2_int <- function(cena, item_SKU){
  0.6 + cena * df$item_SKU + 0.4
}

same_picking_2 <- function(dt, cena){
  dt[, price_model2 := same_picking_2_int(cena, item_SKU)]
}

# Use update by reference
dt <- data.table(df)
head(dt, 2)
same_picking_2(dt, 1)
head(dt, 2)

# Slightly more readable, the same output, also utilizes the update by reference of data.table (see tracemem())
dt <- data.table(df)
tracemem(dt)
head(dt, 2)
dt <- same_picking_2(dt, 1)
head(dt, 2)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your input. Your solution does return the outcome I was expecting. However I would like to solve the whole process within the function. Because I am expecting to be changing the data a lot and this function should be able to rewrite the data by itself...
thanks! but check this out: data_model2$price_model2 <<- 0.6 + cena * data_model2$item_SKU + 0.4 If you use a <<- operator you can access the object inside the dataframe. Very clean solution
@AndražPoje, 1. You are on the shortest way to a nice, obfuscated, non-reusable code. 2. Are you sure you will never change the data.frame object (or even it's name)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.