3

I have a data table that can be created by

top_m_dt = data.table(future_fip = c(102, 104, 503),
                  model = c("model_1", "model_2", "model_3"),
                  time_period = c("F1", "F2", "F3"),
                  emission = c("emission_1", "emission_2", "emission_3"),
                  top_1_fip = c(666, 666, 666),
                  top_2_fip = c(666, 666, 666),
                  top_3_fip = c(666, 666, 666))

I would like to replace row 1 and columns top_1_fip, top_2_fip, top_3_fip with a set of values, lets say c(10, 20, 30).

None of the followings work:

top_m_dt[1, c("top_1_fip", "top_2_fip", "top_3_fip")] <- 
    as.numeric(analog_dat_F1$analog_NNs_county[1:3])`

OR

top_m_dt[1, 5:7 ] <- list(analog_dat_F1$analog_NNs_county[1:3])[[1]]

OR

top_m_dt[1, (c("top_1_fip", "top_2_fip", "top_3_fip")):= 
             analog_dat_F1$analog_NNs_county[1:3]] `

They all replace 10 in all three locations. any suggestions?

2
  • Your example code doesn't run---do you mean to have each of the target_fip and model_n and emission values in quotes? Commented May 21, 2019 at 22:44
  • Sorry, the last parentheses was left out of {code} mode! And, OK, those emissions have values in my computer! so, yeah, lets put quotes on them. sorry. Commented May 21, 2019 at 23:01

1 Answer 1

3

data.table syntax is bit different and not everything that works on a dataframe works on data.table. In this case to replace values from specific row and column you could use as.list

library(data.table)
top_m_dt[1, c("top_1_fip", "top_2_fip", "top_3_fip") := as.list(c(10, 20, 30))]

top_m_dt
#   future_fip   model time_period emission top_1_fip top_2_fip top_3_fip
#1: target_fip model_n          F1 emission        10        20        30
#2: target_fip model_n          F2 emission       666       666       666
#3: target_fip model_n          F3 emission       666       666       666
Sign up to request clarification or add additional context in comments.

1 Comment

I was missing as. before the list. This shit is sensitive to everything!

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.