[R] Loop from List in Geospatial Modeling Environment
Does anyone know how to loop in GME using a list of text values. I am trying to create home ranges for each animal in a point shapefile of locations. Here is an example of the Command Text I am trying to use: l-'SGF4037', 'SGF4244'; for (i in ls()){ kde(in=paste(SGF.shp), out=paste(i,_SGF.img), bandwidth=CVh, cellsize=50, where=paste(”BAND=,ls())); }; l is my list attributes in the BAND field. -- View this message in context: http://r.789695.n4.nabble.com/Loop-from-List-in-Geospatial-Modeling-Environment-tp4641149.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop in list
Hello, Sorry, forgot to Cc the list. Em 01-07-2012 01:24, R. Michael Weylandt michael.weyla...@gmail.com escreveu: I might think replicate() is slightly more idiomatic, but I'm not in a position to check if simplify=FALSE will keep a list. It does: class(replicate(20, f(1))) # matrix class(replicate(20, f(1), simplify=FALSE)) # list Rui Barradas Best, Michael On Jun 30, 2012, at 7:13 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, You can avoid the loop using lapply. f - function(x) sample(100, 10) samp.list - lapply(1:20, f) will choose 20 samples of 10 integers up to 100 and put them in a list. All you need is to write a function f(). f() must have an argument, even if it doesn't use it. If you need other arguments to be processed by f(), define it and call it as, for instance using the example above, f - function(x, ...) sample(100, 10, ...) lapply(1:20, f) # the same lapply(1:20, f, replace=TRUE) # a second argument See ?lapply Hope this helps, Rui Barradas Em 30-06-2012 20:34, solafah bh escreveu: Hello I have a loop to sample 20 samples and I want to put them in one list, how I can make this?? Regards Sulafah [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] loop in list
Hello I have a loop to sample 20 samples and I want to put them in one list, how I can make this?? Regards Sulafah [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop in list
Instead of a loop you can use the replicate or lapply functions which will create lists for you. otherwise you can start with an empty list (mylist - list() ) then add to the list in each iteration of the loop: for(i in 1:10) { mylist[[i]] - myfunction(i) } On Sat, Jun 30, 2012 at 1:34 PM, solafah bh solafa...@yahoo.com wrote: Hello I have a loop to sample 20 samples and I want to put them in one list, how I can make this?? Regards Sulafah [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop in list
Hi, Try this, list1-list() vec-rnorm(15,25) for(i in 1:20) { list1[[i]]-sample(vec,replace=FALSE) } list1 [[1]] [1] 24.28594 25.05309 25.48962 24.71479 22.48122 25.41300 25.26129 25.15602 [9] 24.91442 23.65078 26.84776 24.85934 25.00111 24.16320 27.05351 [[2]] [1] 24.91442 24.28594 25.05309 24.16320 24.71479 22.48122 25.26129 26.84776 [9] 25.00111 25.41300 27.05351 25.48962 25.15602 24.85934 23.65078 --- A.K. - Original Message - From: solafah bh solafa...@yahoo.com To: R help mailing list r-help@r-project.org Cc: Sent: Saturday, June 30, 2012 3:34 PM Subject: [R] loop in list Hello I have a loop to sample 20 samples and I want to put them in one list, how I can make this?? Regards Sulafah [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop in list
Hello, You can avoid the loop using lapply. f - function(x) sample(100, 10) samp.list - lapply(1:20, f) will choose 20 samples of 10 integers up to 100 and put them in a list. All you need is to write a function f(). f() must have an argument, even if it doesn't use it. If you need other arguments to be processed by f(), define it and call it as, for instance using the example above, f - function(x, ...) sample(100, 10, ...) lapply(1:20, f) # the same lapply(1:20, f, replace=TRUE) # a second argument See ?lapply Hope this helps, Rui Barradas Em 30-06-2012 20:34, solafah bh escreveu: Hello I have a loop to sample 20 samples and I want to put them in one list, how I can make this?? Regards Sulafah [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop in list
I might think replicate() is slightly more idiomatic, but I'm not in a position to check if simplify=FALSE will keep a list. Best, Michael On Jun 30, 2012, at 7:13 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, You can avoid the loop using lapply. f - function(x) sample(100, 10) samp.list - lapply(1:20, f) will choose 20 samples of 10 integers up to 100 and put them in a list. All you need is to write a function f(). f() must have an argument, even if it doesn't use it. If you need other arguments to be processed by f(), define it and call it as, for instance using the example above, f - function(x, ...) sample(100, 10, ...) lapply(1:20, f) # the same lapply(1:20, f, replace=TRUE) # a second argument See ?lapply Hope this helps, Rui Barradas Em 30-06-2012 20:34, solafah bh escreveu: Hello I have a loop to sample 20 samples and I want to put them in one list, how I can make this?? Regards Sulafah [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop on list levels and names
I didn't know about crantastic actually. I've looked what it is exactly and it indeed looks interesting, but I don't really see how I would know that it would help me for the task. There's a description of what it was built for, but how can I then know which function from this package can help me? Thanks for your answer (you all), I'll work on it! I'll keep you informed if it doesn't work (!), and I'll go vote on crantastic when I'll have a bit more experience with the packages I use (right now I'm just using the ones I was told for one specific function), but don't worry I won't forget. As you said It only works if users contribute to it. That makes the power of R! Ivan Le 1/21/2010 19:01, Matthew Dowle a écrit : One way is : dataset = data.table(ssfamed) dataset[, whatever some functions are on Asfc, Smc, epLsar, etc, by=SPECSHOR,BONE] Your SPECSHOR and BONE names will be in your result alongside the results of thewhatever ... Or try package plyr which does this sort of thing too. And sqldf may be better if you know SQL and prefer it. There are actually zillions of ways to do it : by(), doBy() etc etc If you get your code working the way its constructed currently, its going to be very slow, because of those ==. data.table doesn't do that and is pretty fast for this kind of thing. You might find that plyr is easier to use and more flexible though if speed isn't an issue, depending on exactly what you want to do. Whichever way you decide, consider voting on crantastic for the package you end up using, and that may be a quick and easy way for you to help new R users in the future, and help us all by reducing the r-help traffic on the same subject over and over again. Note that plyr is the 2nd spot on crantastic, it would have solved your problem without needing to write that code. If you check crantastic first and make sure you're aware of popular packages, it might avoid getting stuck in this way again. It only works if users contribute to it though. Ivan Calandraivan.calan...@uni-hamburg.de wrote in message news:4b587cdd.4070...@uni-hamburg.de... Hi everybody! To use some functions, I have to transform my dataset into a list, where each element contains one group, and I have to prepare a list for each variable I have (altogether I have 15 variables, and many entries per factor level) Here is some part of my dataset: SPECSHORBONEAsfcSmcepLsar cotautx454.39036929.2616380.001136 cotautx117.4457114.2918840.00056 cotautx381.02468215.3130170.002324 cotautx159.08178918.1345330.000462 cotautm160.6415036.4113320.000571 cotautm79.2380233.8282540.001182 cotautm143.2065511.9218990.000192 cotautm115.47699633.1163860.000417 cotautm594.25623472.5381310.000477 eqgretx188.2613248.2790960.000777 eqgretx152.4442162.5963250.001022 eqgretx256.6015078.2790960.000566 eqgretx250.81644518.1345330.000535 eqgretx272.39671124.4928790.000585 eqgretm172.632644.2918840.001781 eqgretm189.44109714.4254980.001347 eqgretm170.74378813.5644720.000602 eqgretm158.96084910.3852990.001189 eqgretm80.9724083.8282540.000644 gicamtx294.4940019.6567380.000524 gicamtx267.12676519.1280240.000647 gicamtx81.8886584.7820060.000492 gicamtx168.3290812.7299390.001097 gicamtx123.2960567.0074270.000659 gicamtm94.26488718.1345330.000752 gicamtm54.3173953.8282540.00038 gicamtm55.97888317.1675340.000141 gicamtm279.59799315.3130170.000398 gicamtm288.26255618.1345330.001043 What I do next is: list_Asfc- list() list_Asfc[[1]]- ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tx', 3] list_Asfc[[2]]- ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tm', 3] And so on for each level of SPECSHOR and BONE I'm stuck on 2 parts: - in a loop or something similar, I would like the 1st element of the list to be filled by the values for the 1st variable with the first level of my factors (i.e. cotau + tx), and then the 2nd element with the 2nd level (i.e. cotau + tm) and so on. As shown above, I know how to do it if I enter manually the different levels, but I have no idea which function I should use so that each combination of factor will be used. See what I mean? - I would then like to run it in a loop or something for each variable. It is by itself not so complicated, but I don't know how to give the correct name to my list. I want the list containing the data for Asfc to be named list_Asfc. Here is what I tried: seq.num- c(seq(3,5,1)) #the indexes of the variables for(i in 1:length(seq.num)) { k- seq.num[i] name.num- names(ssfamed)[k]
Re: [R] loop on list levels and names
Without reading all the details of your question, it looks like maybe split() is what you want. split( dataset, paste(dataset$SPECSHOR,dataset$BONE) ) or split( dataset[,3], paste(dataset$SPECSHOR,dataset$BONE) ) -Don At 5:12 PM +0100 1/21/10, Ivan Calandra wrote: Hi everybody! To use some functions, I have to transform my dataset into a list, where each element contains one group, and I have to prepare a list for each variable I have (altogether I have 15 variables, and many entries per factor level) Here is some part of my dataset: SPECSHORBONEAsfcSmcepLsar cotautx454.39036929.2616380.001136 cotautx117.4457114.2918840.00056 cotautx381.02468215.3130170.002324 cotautx159.08178918.1345330.000462 cotautm160.6415036.4113320.000571 cotautm79.2380233.8282540.001182 cotautm143.2065511.9218990.000192 cotautm115.47699633.1163860.000417 cotautm594.25623472.5381310.000477 eqgretx188.2613248.2790960.000777 eqgretx152.4442162.5963250.001022 eqgretx256.6015078.2790960.000566 eqgretx250.81644518.1345330.000535 eqgretx272.39671124.4928790.000585 eqgretm172.632644.2918840.001781 eqgretm189.44109714.4254980.001347 eqgretm170.74378813.5644720.000602 eqgretm158.96084910.3852990.001189 eqgretm80.9724083.8282540.000644 gicamtx294.4940019.6567380.000524 gicamtx267.12676519.1280240.000647 gicamtx81.8886584.7820060.000492 gicamtx168.3290812.7299390.001097 gicamtx123.2960567.0074270.000659 gicamtm94.26488718.1345330.000752 gicamtm54.3173953.8282540.00038 gicamtm55.97888317.1675340.000141 gicamtm279.59799315.3130170.000398 gicamtm288.26255618.1345330.001043 What I do next is: list_Asfc - list() list_Asfc[[1]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tx', 3] list_Asfc[[2]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tm', 3] And so on for each level of SPECSHOR and BONE I'm stuck on 2 parts: - in a loop or something similar, I would like the 1st element of the list to be filled by the values for the 1st variable with the first level of my factors (i.e. cotau + tx), and then the 2nd element with the 2nd level (i.e. cotau + tm) and so on. As shown above, I know how to do it if I enter manually the different levels, but I have no idea which function I should use so that each combination of factor will be used. See what I mean? - I would then like to run it in a loop or something for each variable. It is by itself not so complicated, but I don't know how to give the correct name to my list. I want the list containing the data for Asfc to be named list_Asfc. Here is what I tried: seq.num - c(seq(3,5,1)) #the indexes of the variables for(i in 1:length(seq.num)) { k - seq.num[i] name.num - names(ssfamed)[k] list - list() list[[1]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tx', i] list[[2]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tm', i] names(list) - c(cotau_tx, cotau_tm) #I have more and the 1st question should help me on that too } After names(list) I need to insert something like: name_list - list But I don't know how to give it the correct name. How do we change the name of an object? Or am I on the wrong path? Thank you in advance for your help. Ivan PS: if necessary: under Windows XP, R2.10. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://*stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://*www.*R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop on list levels and names
Great. If you mean the crantastic r package, sorry I wasn't clear, I meant the crantastic website http://crantastic.org/. If you meant the description of plyr then if the description looks useful then click the link taking you to the package documentation and read it. Same for any of the other packages. The idea, I think, is that its a good idea to make yourself aware of the most popular packages i.e. perhaps just read the descriptions of the top 30 or something like that maybe. Maybe it helps you avoid re-inventing the wheel. That seems to be the case here. Re Don's reply, sure you can use split(). But that will use more memory. And using paste for this? Ok, it works, but don't you want to use better ways? data.table should be much faster and more convenient, quicker to write than split and paste like that. HTH Ivan Calandra ivan.calan...@uni-hamburg.de wrote in message news:4b59bdc5.60...@uni-hamburg.de... I didn't know about crantastic actually. I've looked what it is exactly and it indeed looks interesting, but I don't really see how I would know that it would help me for the task. There's a description of what it was built for, but how can I then know which function from this package can help me? Thanks for your answer (you all), I'll work on it! I'll keep you informed if it doesn't work (!), and I'll go vote on crantastic when I'll have a bit more experience with the packages I use (right now I'm just using the ones I was told for one specific function), but don't worry I won't forget. As you said It only works if users contribute to it. That makes the power of R! Ivan Le 1/21/2010 19:01, Matthew Dowle a écrit : One way is : dataset = data.table(ssfamed) dataset[, whatever some functions are on Asfc, Smc, epLsar, etc, by=SPECSHOR,BONE] Your SPECSHOR and BONE names will be in your result alongside the results of thewhatever ... Or try package plyr which does this sort of thing too. And sqldf may be better if you know SQL and prefer it. There are actually zillions of ways to do it : by(), doBy() etc etc If you get your code working the way its constructed currently, its going to be very slow, because of those ==. data.table doesn't do that and is pretty fast for this kind of thing. You might find that plyr is easier to use and more flexible though if speed isn't an issue, depending on exactly what you want to do. Whichever way you decide, consider voting on crantastic for the package you end up using, and that may be a quick and easy way for you to help new R users in the future, and help us all by reducing the r-help traffic on the same subject over and over again. Note that plyr is the 2nd spot on crantastic, it would have solved your problem without needing to write that code. If you check crantastic first and make sure you're aware of popular packages, it might avoid getting stuck in this way again. It only works if users contribute to it though. Ivan Calandraivan.calan...@uni-hamburg.de wrote in message news:4b587cdd.4070...@uni-hamburg.de... Hi everybody! To use some functions, I have to transform my dataset into a list, where each element contains one group, and I have to prepare a list for each variable I have (altogether I have 15 variables, and many entries per factor level) Here is some part of my dataset: SPECSHORBONEAsfcSmcepLsar cotautx454.39036929.2616380.001136 cotautx117.4457114.2918840.00056 cotautx381.02468215.3130170.002324 cotautx159.08178918.1345330.000462 cotautm160.6415036.4113320.000571 cotautm79.2380233.8282540.001182 cotautm143.2065511.9218990.000192 cotautm115.47699633.1163860.000417 cotautm594.25623472.5381310.000477 eqgretx188.2613248.2790960.000777 eqgretx152.4442162.5963250.001022 eqgretx256.6015078.2790960.000566 eqgretx250.81644518.1345330.000535 eqgretx272.39671124.4928790.000585 eqgretm172.632644.2918840.001781 eqgretm189.44109714.4254980.001347 eqgretm170.74378813.5644720.000602 eqgretm158.96084910.3852990.001189 eqgretm80.9724083.8282540.000644 gicamtx294.4940019.6567380.000524 gicamtx267.12676519.1280240.000647 gicamtx81.8886584.7820060.000492 gicamtx168.3290812.7299390.001097 gicamtx123.2960567.0074270.000659 gicamtm94.26488718.1345330.000752 gicamtm54.3173953.8282540.00038 gicamtm55.97888317.1675340.000141 gicamtm279.59799315.3130170.000398 gicamtm288.26255618.1345330.001043 What I do next is: list_Asfc- list() list_Asfc[[1]]-
Re: [R] loop on list levels and names
Thanks for your advice, I will work on it then! Just one last question. In which package can I find the function data.table? Ivan Le 1/22/2010 17:18, Matthew Dowle a écrit : Great. If you mean the crantastic r package, sorry I wasn't clear, I meant the crantastic website http://crantastic.org/. If you meant the description of plyr then if the description looks useful then click the link taking you to the package documentation and read it. Same for any of the other packages. The idea, I think, is that its a good idea to make yourself aware of the most popular packages i.e. perhaps just read the descriptions of the top 30 or something like that maybe. Maybe it helps you avoid re-inventing the wheel. That seems to be the case here. Re Don's reply, sure you can use split(). But that will use more memory. And using paste for this? Ok, it works, but don't you want to use better ways? data.table should be much faster and more convenient, quicker to write than split and paste like that. HTH Ivan Calandraivan.calan...@uni-hamburg.de wrote in message news:4b59bdc5.60...@uni-hamburg.de... I didn't know about crantastic actually. I've looked what it is exactly and it indeed looks interesting, but I don't really see how I would know that it would help me for the task. There's a description of what it was built for, but how can I then know which function from this package can help me? Thanks for your answer (you all), I'll work on it! I'll keep you informed if it doesn't work (!), and I'll go vote on crantastic when I'll have a bit more experience with the packages I use (right now I'm just using the ones I was told for one specific function), but don't worry I won't forget. As you said It only works if users contribute to it. That makes the power of R! Ivan Le 1/21/2010 19:01, Matthew Dowle a écrit : One way is : dataset = data.table(ssfamed) dataset[, whatever some functions are on Asfc, Smc, epLsar, etc, by=SPECSHOR,BONE] Your SPECSHOR and BONE names will be in your result alongside the results of thewhatever ... Or try package plyr which does this sort of thing too. And sqldf may be better if you know SQL and prefer it. There are actually zillions of ways to do it : by(), doBy() etc etc If you get your code working the way its constructed currently, its going to be very slow, because of those ==. data.table doesn't do that and is pretty fast for this kind of thing. You might find that plyr is easier to use and more flexible though if speed isn't an issue, depending on exactly what you want to do. Whichever way you decide, consider voting on crantastic for the package you end up using, and that may be a quick and easy way for you to help new R users in the future, and help us all by reducing the r-help traffic on the same subject over and over again. Note that plyr is the 2nd spot on crantastic, it would have solved your problem without needing to write that code. If you check crantastic first and make sure you're aware of popular packages, it might avoid getting stuck in this way again. It only works if users contribute to it though. Ivan Calandraivan.calan...@uni-hamburg.de wrote in message news:4b587cdd.4070...@uni-hamburg.de... Hi everybody! To use some functions, I have to transform my dataset into a list, where each element contains one group, and I have to prepare a list for each variable I have (altogether I have 15 variables, and many entries per factor level) Here is some part of my dataset: SPECSHORBONEAsfcSmcepLsar cotautx454.39036929.2616380.001136 cotautx117.4457114.2918840.00056 cotautx381.02468215.3130170.002324 cotautx159.08178918.1345330.000462 cotautm160.6415036.4113320.000571 cotautm79.2380233.8282540.001182 cotautm143.2065511.9218990.000192 cotautm115.47699633.1163860.000417 cotautm594.25623472.5381310.000477 eqgretx188.2613248.2790960.000777 eqgretx152.4442162.5963250.001022 eqgretx256.6015078.2790960.000566 eqgretx250.81644518.1345330.000535 eqgretx272.39671124.4928790.000585 eqgretm172.632644.2918840.001781 eqgretm189.44109714.4254980.001347 eqgretm170.74378813.5644720.000602 eqgretm158.96084910.3852990.001189 eqgretm80.9724083.8282540.000644 gicamtx294.4940019.6567380.000524 gicamtx267.12676519.1280240.000647 gicamtx81.8886584.7820060.000492 gicamtx168.3290812.7299390.001097 gicamtx123.2960567.0074270.000659 gicamtm94.26488718.1345330.000752 gicamtm54.3173953.8282540.00038 gicamtm55.97888317.1675340.000141 gicamtm279.59799315.3130170.000398 gicamtm288.262556
Re: [R] loop on list levels and names
data.table is the package name too. Make sure you find ?[.data.table which is linked from ?data.table. You could just do a mean of one variable first, and then build it up from there e.g. dataset[, mean(epLsar), by=SPECSHOR,BONE]. To get multiple columns of output, wrap with DT() like this dataset[, DT(mean(epLsar),min(epLsar)), by=SPECSHOR,BONE] Btw, v1.3 on r-forge fixes a version check warning with v1.2 on R2.10+ (not fixed by me but thanks to a contributor) so if you can't live with the warning messages, you can install v1.3 from r-forge like this : install.packages(data.table,repos=http://r-forge.r-project.org;) Best of luck. Ivan Calandra ivan.calan...@uni-hamburg.de wrote in message news:4b59d93c.5080...@uni-hamburg.de... Thanks for your advice, I will work on it then! Just one last question. In which package can I find the function data.table? Ivan Le 1/22/2010 17:18, Matthew Dowle a écrit : Great. If you mean the crantastic r package, sorry I wasn't clear, I meant the crantastic website http://crantastic.org/. If you meant the description of plyr then if the description looks useful then click the link taking you to the package documentation and read it. Same for any of the other packages. The idea, I think, is that its a good idea to make yourself aware of the most popular packages i.e. perhaps just read the descriptions of the top 30 or something like that maybe. Maybe it helps you avoid re-inventing the wheel. That seems to be the case here. Re Don's reply, sure you can use split(). But that will use more memory. And using paste for this? Ok, it works, but don't you want to use better ways? data.table should be much faster and more convenient, quicker to write than split and paste like that. HTH Ivan Calandraivan.calan...@uni-hamburg.de wrote in message news:4b59bdc5.60...@uni-hamburg.de... I didn't know about crantastic actually. I've looked what it is exactly and it indeed looks interesting, but I don't really see how I would know that it would help me for the task. There's a description of what it was built for, but how can I then know which function from this package can help me? Thanks for your answer (you all), I'll work on it! I'll keep you informed if it doesn't work (!), and I'll go vote on crantastic when I'll have a bit more experience with the packages I use (right now I'm just using the ones I was told for one specific function), but don't worry I won't forget. As you said It only works if users contribute to it. That makes the power of R! Ivan Le 1/21/2010 19:01, Matthew Dowle a écrit : One way is : dataset = data.table(ssfamed) dataset[, whatever some functions are on Asfc, Smc, epLsar, etc, by=SPECSHOR,BONE] Your SPECSHOR and BONE names will be in your result alongside the results of thewhatever ... Or try package plyr which does this sort of thing too. And sqldf may be better if you know SQL and prefer it. There are actually zillions of ways to do it : by(), doBy() etc etc If you get your code working the way its constructed currently, its going to be very slow, because of those ==. data.table doesn't do that and is pretty fast for this kind of thing. You might find that plyr is easier to use and more flexible though if speed isn't an issue, depending on exactly what you want to do. Whichever way you decide, consider voting on crantastic for the package you end up using, and that may be a quick and easy way for you to help new R users in the future, and help us all by reducing the r-help traffic on the same subject over and over again. Note that plyr is the 2nd spot on crantastic, it would have solved your problem without needing to write that code. If you check crantastic first and make sure you're aware of popular packages, it might avoid getting stuck in this way again. It only works if users contribute to it though. Ivan Calandraivan.calan...@uni-hamburg.de wrote in message news:4b587cdd.4070...@uni-hamburg.de... Hi everybody! To use some functions, I have to transform my dataset into a list, where each element contains one group, and I have to prepare a list for each variable I have (altogether I have 15 variables, and many entries per factor level) Here is some part of my dataset: SPECSHORBONEAsfcSmcepLsar cotautx454.39036929.2616380.001136 cotautx117.4457114.2918840.00056 cotautx381.02468215.3130170.002324 cotautx159.08178918.1345330.000462 cotautm160.6415036.4113320.000571 cotautm79.2380233.8282540.001182 cotautm143.2065511.9218990.000192 cotautm115.47699633.1163860.000417 cotautm594.25623472.5381310.000477 eqgretx188.2613248.2790960.000777 eqgretx152.4442162.5963250.001022 eqgretx256.6015078.2790960.000566 eqgre
[R] loop on list levels and names
Hi everybody! To use some functions, I have to transform my dataset into a list, where each element contains one group, and I have to prepare a list for each variable I have (altogether I have 15 variables, and many entries per factor level) Here is some part of my dataset: SPECSHORBONEAsfcSmcepLsar cotautx454.39036929.2616380.001136 cotautx117.4457114.2918840.00056 cotautx381.02468215.3130170.002324 cotautx159.08178918.1345330.000462 cotautm160.6415036.4113320.000571 cotautm79.2380233.8282540.001182 cotautm143.2065511.9218990.000192 cotautm115.47699633.1163860.000417 cotautm594.25623472.5381310.000477 eqgretx188.2613248.2790960.000777 eqgretx152.4442162.5963250.001022 eqgretx256.6015078.2790960.000566 eqgretx250.81644518.1345330.000535 eqgretx272.39671124.4928790.000585 eqgretm172.632644.2918840.001781 eqgretm189.44109714.4254980.001347 eqgretm170.74378813.5644720.000602 eqgretm158.96084910.3852990.001189 eqgretm80.9724083.8282540.000644 gicamtx294.4940019.6567380.000524 gicamtx267.12676519.1280240.000647 gicamtx81.8886584.7820060.000492 gicamtx168.3290812.7299390.001097 gicamtx123.2960567.0074270.000659 gicamtm94.26488718.1345330.000752 gicamtm54.3173953.8282540.00038 gicamtm55.97888317.1675340.000141 gicamtm279.59799315.3130170.000398 gicamtm288.26255618.1345330.001043 What I do next is: list_Asfc - list() list_Asfc[[1]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tx', 3] list_Asfc[[2]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tm', 3] And so on for each level of SPECSHOR and BONE I'm stuck on 2 parts: - in a loop or something similar, I would like the 1st element of the list to be filled by the values for the 1st variable with the first level of my factors (i.e. cotau + tx), and then the 2nd element with the 2nd level (i.e. cotau + tm) and so on. As shown above, I know how to do it if I enter manually the different levels, but I have no idea which function I should use so that each combination of factor will be used. See what I mean? - I would then like to run it in a loop or something for each variable. It is by itself not so complicated, but I don't know how to give the correct name to my list. I want the list containing the data for Asfc to be named list_Asfc. Here is what I tried: seq.num - c(seq(3,5,1)) #the indexes of the variables for(i in 1:length(seq.num)) { k - seq.num[i] name.num - names(ssfamed)[k] list - list() list[[1]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tx', i] list[[2]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tm', i] names(list) - c(cotau_tx, cotau_tm) #I have more and the 1st question should help me on that too } After names(list) I need to insert something like: name_list - list But I don't know how to give it the correct name. How do we change the name of an object? Or am I on the wrong path? Thank you in advance for your help. Ivan PS: if necessary: under Windows XP, R2.10. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop on list levels and names
One way is : dataset = data.table(ssfamed) dataset[, whatever some functions are on Asfc, Smc, epLsar, etc , by=SPECSHOR,BONE] Your SPECSHOR and BONE names will be in your result alongside the results of the whatever ... Or try package plyr which does this sort of thing too. And sqldf may be better if you know SQL and prefer it. There are actually zillions of ways to do it : by(), doBy() etc etc If you get your code working the way its constructed currently, its going to be very slow, because of those ==. data.table doesn't do that and is pretty fast for this kind of thing. You might find that plyr is easier to use and more flexible though if speed isn't an issue, depending on exactly what you want to do. Whichever way you decide, consider voting on crantastic for the package you end up using, and that may be a quick and easy way for you to help new R users in the future, and help us all by reducing the r-help traffic on the same subject over and over again. Note that plyr is the 2nd spot on crantastic, it would have solved your problem without needing to write that code. If you check crantastic first and make sure you're aware of popular packages, it might avoid getting stuck in this way again. It only works if users contribute to it though. Ivan Calandra ivan.calan...@uni-hamburg.de wrote in message news:4b587cdd.4070...@uni-hamburg.de... Hi everybody! To use some functions, I have to transform my dataset into a list, where each element contains one group, and I have to prepare a list for each variable I have (altogether I have 15 variables, and many entries per factor level) Here is some part of my dataset: SPECSHORBONEAsfcSmcepLsar cotautx454.39036929.2616380.001136 cotautx117.4457114.2918840.00056 cotautx381.02468215.3130170.002324 cotautx159.08178918.1345330.000462 cotautm160.6415036.4113320.000571 cotautm79.2380233.8282540.001182 cotautm143.2065511.9218990.000192 cotautm115.47699633.1163860.000417 cotautm594.25623472.5381310.000477 eqgretx188.2613248.2790960.000777 eqgretx152.4442162.5963250.001022 eqgretx256.6015078.2790960.000566 eqgretx250.81644518.1345330.000535 eqgretx272.39671124.4928790.000585 eqgretm172.632644.2918840.001781 eqgretm189.44109714.4254980.001347 eqgretm170.74378813.5644720.000602 eqgretm158.96084910.3852990.001189 eqgretm80.9724083.8282540.000644 gicamtx294.4940019.6567380.000524 gicamtx267.12676519.1280240.000647 gicamtx81.8886584.7820060.000492 gicamtx168.3290812.7299390.001097 gicamtx123.2960567.0074270.000659 gicamtm94.26488718.1345330.000752 gicamtm54.3173953.8282540.00038 gicamtm55.97888317.1675340.000141 gicamtm279.59799315.3130170.000398 gicamtm288.26255618.1345330.001043 What I do next is: list_Asfc - list() list_Asfc[[1]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tx', 3] list_Asfc[[2]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tm', 3] And so on for each level of SPECSHOR and BONE I'm stuck on 2 parts: - in a loop or something similar, I would like the 1st element of the list to be filled by the values for the 1st variable with the first level of my factors (i.e. cotau + tx), and then the 2nd element with the 2nd level (i.e. cotau + tm) and so on. As shown above, I know how to do it if I enter manually the different levels, but I have no idea which function I should use so that each combination of factor will be used. See what I mean? - I would then like to run it in a loop or something for each variable. It is by itself not so complicated, but I don't know how to give the correct name to my list. I want the list containing the data for Asfc to be named list_Asfc. Here is what I tried: seq.num - c(seq(3,5,1)) #the indexes of the variables for(i in 1:length(seq.num)) { k - seq.num[i] name.num - names(ssfamed)[k] list - list() list[[1]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tx', i] list[[2]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tm', i] names(list) - c(cotau_tx, cotau_tm) #I have more and the 1st question should help me on that too } After names(list) I need to insert something like: name_list - list But I don't know how to give it the correct name. How do we change the name of an object? Or am I on the wrong path? Thank you in advance for your help. Ivan PS: if necessary: under Windows XP, R2.10. [[alternative HTML version deleted]] __