0

I need to replace this code and Dynamically assign rep(NA, n) for each attribute and value. How can I do it ? I tried below code but doesn't work. Please help.

This is the code need to be replaced -

Attribute_1_Value = Attribute_1_Name = Attribute_2_Value = Attribute_2_Name = Attribute_3_Value = Attribute_3_Name = rep(NA,n)

This is the code I tried but didn't work. It gives me error "value for ‘Attribute_1_Name’ not found "

for(i in 1:3) 
  { 
    tempName <- str_replace("Attribute_%d_Name", "%d", i)  
    tempName = rep(NA,n)

    tempValue <- str_replace("Attribute_%d_Name", "%d", i)  
    tempValue = rep(NA,n)
  }
Partha
  • 57
  • 1
  • 7
  • 1
    I don't think this is a very clear question. Can you provide what the data looks like currently and and what you'd like it to look like? – maloneypatr Jan 22 '16 at 21:07
  • In addition to clearing up the question, what is `n` ? – steveb Jan 22 '16 at 21:09
  • n is a integer variable. You can consider is as 5. - Thanks. – Partha Jan 22 '16 at 21:13
  • All I'm asking is rather than writing Attribute_1_Value = Attribute_1_Name = ........... n number of times I need to automate it. How can I do it ? – Partha Jan 22 '16 at 21:15
  • `i <- 1:5; paste0('Attribute_', i, '_Value = Attribute_', i, '_Name', collapse = ' = ')` – maloneypatr Jan 22 '16 at 21:19
  • Very Close. This gives me : Attribute_1_Value = Attribute_1_Name = Attribute_2_Value = Attribute_2_Name = Attribute_3_Value = Attribute_3_Name = Attribute_4_Value = Attribute_4_Name = Attribute_5_Value = Attribute_5_Name But I'm looking for .................. Attribute_1_Value = Attribute_1_Name = Attribute_2_Value = Attribute_2_Name = Attribute_3_Value = Attribute_3_Name = Attribute_4_Value = Attribute_4_Name = Attribute_5_Value = Attribute_5_Name = rep(NA,n) – Partha Jan 22 '16 at 21:26
  • Also.. I'm not exactly asking to paste, rather trying to assign values dynamically.. something like this .... How can I do it ? Attribute_1_Value = Attribute_1_Name = rep(NA,n) Attribute_2_Value = Attribute_2_Name = rep(NA,n) Attribute_3_Value = Attribute_3_Name = rep(NA,n) – Partha Jan 22 '16 at 22:11
  • This is the Answer of my question. I could able to fix the solution. – Partha Jan 25 '16 at 15:17
  • for(i in 1:length(dnvParameterVector)) { nam1 <- paste("Attribute_", i, "_Name", sep = "") assign(nam1, attributeNames[i]) print(nam1) } for(i in 1:length(dnvParameterVector)) { nam2 <- paste("Attribute_", i, "_Value", sep = "") assign(nam2, rep(attributeFilter[i],n)) print(nam2) } – Partha Jan 25 '16 at 15:17

0 Answers0