-1

I would like to generate series of vectors like india_a, india_b, india_c. These vectors will have length 3. For example, 1st entry in india_a will be summation of 'total'when yrs=1 and crs=a.

for (i in crimes){   assign(paste("india_",i,sep=""),rep(NA,12))   
for (j in 1:12){
    india_i[j] <-sum(juvenile_crime$total[
                  juvenile_crime$year==years[j]&juvenile_crime$crime==crimes[i]]) } }

this is the message I get when I run above code

Error in india_i[j] <- sum(juvenile_crime$total[juvenile_crime$year == :
object 'india_i' not found

this example might help:

sts <- c(rep("s1",9),rep("s2",9))
yrs <- c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3)
crs <- c("a","b","c","a","b","c","a","b","c")
total <- c(1:18)
cri <- data.frame(sts,yrs,crs,total)
attach(cri)
yr <- levels(cri$yrs)
cr <- levels(cri$crs)
for (i in crs){   
assign(paste("india_",i,sep=""),rep(NA,3))   
for (j in 1:3){
india_i[j] <-sum(total[yrs==yr[j]&crs==cr[i]]
}
}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47

2 Answers2

1

There is no object with the name "india_i". We don't have any information about what is in the "crimes" vector but if it's the numbers 1:12 then the objects have names like "india_1". You should learn to make named lists, rather than using separate objects.

After your edit, we can demonstrate this using a slightly modified version of your code ( and adding the missing close-parenthesis for the sum-call).

India_L <- list()   # create an (empty)  master list
for (i in crs){   
   assign(paste("india_",i,sep=""),rep(NA,3))   
   for (j in 1:3){
       India_L[[paste("india_",j,sep="")]] <-sum(total[yrs==yr[j]&crs==cr[i]])
}
}
India_L  # print to see the structure
#---- result
$india_1
[1] 0

$india_2
[1] 0

$india_3
[1] 0

The reason you got all zeroes was that there are no levels for the yrs column of the cri-object. It was "numeric" and only "factor"-classes have levels in R. A comment on your strategy. I wasn't really sure what goal you had set for yourself (besides) getting the assign function to succeed. The sum of those logical tests didn't seem particularly informative. Perhaps you meant to use the %in% operator. Using == with a vector will not generally be informative.

Using attach is generally very unwise. Notice the warning you got:

> attach(cri)
The following objects are masked _by_ .GlobalEnv:

    crs, sts, total, yrs

The following objects are masked from cri (pos = 3):

    crs, sts, total, yrs

So the objects named might be changed with an edit:

 sts <- c(rep("s1.a",9),rep("s2.a",9))

What object do you think was altered? And if you then detach-ed the cri-dataframe, where do you think the edit would reside? One of the big problems with attaching objects is that the user gets confused about what is actually being changed.

It would be more clear to create the values with the dataframe in one pass and then work on components of the dataframe:

cri <- data.frame(
   sts = c(rep("s1",9),rep("s2",9)),
   yrs = c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3),
   crs = c("a","b","c","a","b","c","a","b","c"),
   total = c(1:18)  )
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    To David and Mike: When the questioner doesn't include the contents or even a description of what is in the `crimes` object, then the most complete answer to the Q: "why doesn't this code work" is to say that the is no object with the name "india_i", and to explain that there are better strategies (and hope that the question can be improved.) Since the questioner did improve the question (which is rather unusual), we can now proceed with an improved answer. (When I propose an answer, it's not a promise that I will hourly monitor future edits.) – IRTFM Dec 28 '15 at 17:21
-1

If you want create 3 vectors, you may do something like this:

for (i in unique(crs)){   
           # Note: only one of each value
   vect <- numeric(3)
           # create a help vector
   for (j in 1:3) {
      vect[j] <-sum(total[yrs==yr[j] & crs==cr[i]])
   }
   assign(paste("india_",i,sep=""), vect)
           # assign this vector to "india_i"
}

However, this program does not work. As yrs is numeric it will be included in the cri data frame as-is, and does not have any levels, and hence yrs==yr[j] is never true.

Another point: it is usually better to use lists instead of assignment of india_i. I would do

india <- vector("list", 3)
names(india) <- letters[1:3]

and the assignment later would be like

india[[i]] <- vect

And please!!! ensure your code runs (except the error you are struggling with.) Currently it does not even load as a parenthesis is missing from india_i[j] <-sum(total[yrs==yr[j]&crs==cr[i]].

Ott Toomet
  • 1,894
  • 15
  • 25