-1

I am quite new in R and I know it is very simple but i got stuck.

Could you please tell me how I can write an Excel formula ="X" & i (for i for instance from 1 to 10) used in loop in r.

For example, assume I have two dataframes with a single column "SUBSET1" and "SUBSET2". What I want is to save the result of the sum of each column in two different dataframes.

For an reproducible example please refer below to the EDIT part:

Illustration:

for (i in 1:2)
 {
 assign(paste0("sum_results", i),"")
 }
for (i in 1:2) 
 {
  sum_results & i<-sum(subset & i) ----something which works in this way
 }

I would be very grateful for any hint.

EDIT: Proper example:

Let's assume I have the following data frames

 a<-c(2,3,4)
 b<-c(2,3,5)
 subset1<-data.frame(a,b)
 a<-c(2,7,5)
 b<-c(4,8,15)
 subset2<-data.frame(a,b)

So desired output is that I have two data frames: sum_results1 & sum_results2, where sum_results1 is the sum of the column "a" of the subset1, and sum_results2 is the sum of the column "a" of the subset2.

 for (i in 1:2)
 {
 assign(paste0("sum_results", i),"")
 }
 for (i in 1:2) 
 {
  sum_results & i<-sum(subset & i)$a --that is where the problem is
 }
Jack
  • 157
  • 2
  • 11
  • 2
    This doesn't seem like a job for loops in R. But without sample data & a desired result, it's tough to say. Here is one guide to creating a reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Sam Firke Dec 11 '15 at 20:52
  • 1
    I think this would be the equivalent to `lapply(data, sum)` but you should include some data and expected output. – jeremycg Dec 11 '15 at 20:54
  • I would recommend against creating variables like that. Use a `list` instead (jeremycg's comment). – Roman Luštrik Dec 12 '15 at 15:15
  • @Jack does my answer not work? – road_to_quantdom Dec 12 '15 at 22:20
  • I am very ssory for late comeback. Was not able to reply. I will expand the question with reproducible example. – Jack Dec 14 '15 at 18:23
  • I have added reproducible example. I have gone through apply family function but do not really know how I can implement it here. – Jack Dec 14 '15 at 18:44
  • 1
    Generally, using indexed variable names is bug-prone, awkward, and difficult to scale. [You should use a list instead](http://stackoverflow.com/q/17499013/903061). – Gregor Thomas Dec 14 '15 at 19:24

1 Answers1

1

you were very close. Assuming I am understanding your question correctly, try this:

for (i in 1:2)
 {
 assign(paste0("sum_results", i),sum(get(paste0("subset",i))))
 }

Generally, you want to avoid loops in R. See the comments to your question regarding lapply There are probably much more efficient ways to solving this question. But you have not provided a replicable example as also mentioned in your comments. But let me know if this helps!

EDIT:: below is how you would use sapply and then my solution above to rename your results. sapply will allow you to use a more complicated function that could potentially do things with more than one column. You will have to be specific.

N <- 2
res <- sapply(1:N, function(i) sum(get(paste0("subset",i))))

for (i in 1:N)
{
  assign(paste0("sum_results", i),res[i])
}
road_to_quantdom
  • 1,211
  • 1
  • 12
  • 20
  • It works properly. Thank you a lot road_to_quantdom. The thing is that I forgot to mention that (and I am very ssory for that!) my data is very huge and I would like to apply sum (or whatever other function) to some specific column (or the input to the function you be from several different columns of one dataframe). Is that possible to do it with formulae you have written? – Jack Dec 14 '15 at 18:43
  • @Jack Yes, just change `get(paste0("subset",i))` with `get(paste0("subset",i))[ , colidx]` where `colidx` are the indices of the columns you want. If you only want one column then `sum` should work as it will sum a column vector. If you are pulling multiple columns , then I am going to need more details. also, if your data is very big, I would avoid using a loop. You should explain very specifically what you want to do in your question – road_to_quantdom Dec 14 '15 at 19:02
  • Thank you a lot road_to_quantdom. Loop has worked. I am currently trying to simplify my sample to show it is as example. Nevertheless, your loop helped. – Jack Dec 15 '15 at 20:12