Re: [R] Looping

2024-02-20 Thread Steven Yen
Steven from iPhone > On Feb 19, 2024, at 4:56 PM, Steven Yen wrote: > > Thanks to all. Glad there are many options. > > Steven from iPhone > >>> On Feb 19, 2024, at 1:55 PM, Rui Barradas wrote: >>> >> Às 03:27 de 19/02/2024, Steven Yen escreveu: >>> I need to read csv files repeatedly, n

Re: [R] Looping

2024-02-19 Thread Marc Girondot via R-help
In my package HelpersMG, I have included a function to read in one time all the files of a folder and they are stored in a list: read_folder(   folder = try(file.choose(), silent = TRUE),   file = NULL,   wildcard = "*.*",   read = read.delim,   ... ) In your case, for example: library("Helper

Re: [R] Looping

2024-02-18 Thread Rui Barradas
Às 03:27 de 19/02/2024, Steven Yen escreveu: I need to read csv files repeatedly, named data1.csv, data2.csv,… data24.csv, 24 altogether. That is, data<-read.csv(“data1.csv”) … data<-read.csv(“data24.csv”) … Is there a way to do this in a loop? Thank you. Steven from iPhone [[alternat

Re: [R] Looping

2024-02-18 Thread Richard O'Keefe
f <- function (filename) { data<- read.csv(filename) .. } for (filename in paste0("data", 1:24, ".csv")) f(filename) Depending on what exactly you have in your file system, for (filename in system("ls data*.csv", TRUE)) f(filename) might work. On Mon, 19 Feb 2024 at 16:33, Steven Yen wrote

Re: [R] Looping

2024-02-18 Thread avi.e.gross
Steven, It depends what you want to do. What you are showing seems to replace the values stored in "data" each time. Many kinds of loops will do that, with one simple way being to store all the filenames in a list and loop on the contents of the list as arguments to read.csv. Since you show f

Re: [R] Looping

2024-02-18 Thread Peter Langfelder
Try for (ind in 1:24) { data = read.csv(paste0("data", ind, ".csv")) ... } Peter On Mon, Feb 19, 2024 at 11:33 AM Steven Yen wrote: > > I need to read csv files repeatedly, named data1.csv, data2.csv,… data24.csv, > 24 altogether. That is, > > data<-read.csv(“data1.csv”) > … > data<-rea

Re: [R] Looping through all matrix columns each 1 row at a time

2022-04-21 Thread Eric Berger
Hi Paul, I am not sure I understand your question, but perhaps the following is helpful. In particular, the apply() function used with MAR=1, applies a function to a matrix row-wise. set.seed(123) m <- matrix(sample(1:6,5*12,replace=TRUE),ncol=12) ## dummy data m [,1] [,2] [,3] [,4] [,5] [,6]

Re: [R] Looping through data error

2021-04-13 Thread Bert Gunter
Well, if I understand your query, wouldn't the following simple approach suffice -- it assumes that the results for each company are ordered by year, as your example seems to show: ## test is your example data ## first remove NA's test2 <- na.omit(test) ## Now just use tapply(): > out <-with(test

Re: [R] Looping through data error

2021-04-13 Thread e-mail ma015k3113 via R-help
Rui, excellent diagnosis and suggestion. It worked but my damn logic is still not delivering what I want-will spend more time on it tomorrow. Kind regards Ahson > On 13 April 2021 at 17:06 Rui Barradas wrote: > > > Hello, > > A close parenthesis is missing in the nd if. > > > for (i in 1

Re: [R] Looping through data error

2021-04-13 Thread e-mail ma015k3113 via R-help
Jim, thanks for taking the time to look into this. Yes, these if else statements are so confusing. I tried your amended scode and it does not work. The error are as follows: Error: unexpected '}' in " }" > NUMBER_OF_SHARES[i] = 100 / is.na(CLOSE_SHARE_PRICE[i]) > } Error: unexpected '}' in " }"

Re: [R] Looping through data error

2021-04-13 Thread Rui Barradas
Hello, Typo, inline. Às 17:06 de 13/04/21, Rui Barradas escreveu: Hello, A close parenthesis is missing in the nd if. Should be "the 2nd if". Rui Barradas for (i in 1:(nrow(PLC_Return)-1)){   if (i == 1){     NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i])   } else if(is.na(PLC

Re: [R] Looping through data error

2021-04-13 Thread Rui Barradas
Hello, A close parenthesis is missing in the nd if. for (i in 1:(nrow(PLC_Return)-1)){ if (i == 1){ NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } else if(is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1])){ NUMBER_OF_SHARES[i]=0 } else { NUMBER_OF_SHARES[i] = 100

Re: [R] Looping through data error

2021-04-13 Thread jim holtman
Your code was formatted incorrectly. There is always a problem with the 'else' statement after an 'if' since in R there is no semicolon to mark the end of a line. Here might be a better format for your code. I would recommend the liberal use of "{}"s when using 'if/else' i <- 0 for (i in 1:(

Re: [R] Looping thorugh dataframe

2020-07-22 Thread William Dunlap via R-help
> library(dplyr, warn.conflicts=FALSE) > d <- data.frame(Company=c("MATH","IFUL","SSI","MATH","MATH","SSI"), > Turnover=c(2,3,5,7,9,11)) > d %>% group_by(Company) %>% summarize(Count=n(), MeanTurnover=mean(Turnover), > TotalTurnover=sum(Turnover)) `summarise()` ungrouping output (override with `.

Re: [R] Looping thorugh dataframe

2020-07-22 Thread Sarah Goslee
Hi, Your sample code suggests that you don't yet understand how R works, and might benefit from a tutorial or two. However, your verbal description of what you want is quite straightforward. Here's a R-style way to count the number of times each company appears, and to get the mean value of Turnov

Re: [R] Looping thorugh dataframe

2020-07-22 Thread e-mail ma015k3113 via R-help
Bert, thanks for responding to my email. I do realise that newbie's like my can expect curt answers but not to worry. I am definitely learning 'R' and what I posted are also statements from R. The statements run perfectly well but don't do what I want them to do. My mistake I have posted sample

Re: [R] Looping through a dataframe

2020-07-21 Thread Jim Lemon
Hi Ahson, Guessing what your data frame might look like, here are two easy ways: All_companies<-data.frame(year=c(1970:2015,2000:2015,2010:2015), COMPANY_NUMBER=c(rep(1,46),rep(2,16),rep(3,6)), COMPANY_NAME=c(rep("IBM",46),rep("AMAZON",16),rep("SPACE-X",6))) # easy ways table(All_companies$COMPA

Re: [R] Looping through a dataframe

2020-07-21 Thread Roger Bos
What you are asking is one area where the package data.table really shines. You didn't provide an example, but based on your question you would do something like: library(data.table) dt <- as.data.table(All_companies) dt[, .N, by=COMPANY_NAME] You will have to read up on data.table, but .N gives

Re: [R] Looping through a dataframe

2020-07-21 Thread John Kane
It occurs to me a simple table command will do what you say you want but I suspect the real analysis is more complicated dat1 <- data.frame(aa = sample(letters[1:5], 10, replace = TRUE), bb = 1:10) table(dat1$aa) On Tue, 21 Jul 2020 at 14:01, John Kane wrote: > As Bert says th

Re: [R] Looping through a dataframe

2020-07-21 Thread John Kane
As Bert says that does not look like R Have a look an these links for some suggestions on asking questions here. http://adv-r.had.co.nz/Reproducibility.html http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example On Tue, 21 Jul 2020 at 13:42, Bert Gunter wrote:

Re: [R] Looping through a dataframe

2020-07-21 Thread Bert Gunter
What language are you programming in? -- it certainly isn't R. I suggest that you stop what you're doing and go through an R tutorial or two before proceeding. This list cannot serve as a substitute for doing such homework (is this homework, btw? -- that's off topic here) nor can we provide such t

Re: [R] Looping with looping

2019-04-19 Thread ani jaya
Thank you very much, Dr. Snow, your suggestion helps a lot. Best, Saat On Fri, Apr 19, 2019 at 4:14 AM Greg Snow <538...@gmail.com> wrote: > When the goal of looping is to compute something and save each > iteration into a vector or list, then it is usually easier to use the > lapply/sapply/repl

Re: [R] Looping with looping

2019-04-18 Thread Greg Snow
When the goal of looping is to compute something and save each iteration into a vector or list, then it is usually easier to use the lapply/sapply/replicate functions and save the result into a single list rather than a bunch of global variables. Here is a quick example that does the same computat

Re: [R] looping using 'diverse' package measures

2017-10-19 Thread David L Carlson
You really need to spend some time learning the basics of R. There are thousands of R packages, so you also need to spend time reading the documentation for the package so that you can show us what the data format should be like. Here are some simple ways to transform the data. You should also

Re: [R] looping using 'diverse' package measures

2017-10-19 Thread Michael Dewey
Dear Li Not absolutely sure what you want to do but try ?aggregate ?by ?apply (or one of the other apply functions, possibly tapply On 19/10/2017 11:29, Li Jiang wrote: Hi everyone, I'm new at R (although I'm a Stata user for some time and somehow proficient in it) and I'm trying to use the '

Re: [R] Looping Through QuantMod Objects

2017-08-02 Thread Joshua Ulrich
Use auto.assign = FALSE in your getFinancials() call, and use viewFinancials() to extract the data you want. items <- c("Cash & Equivalents", "Short Term Investments", "Cash and Short Term Investments") tickers <- c("AAPL", "IBM", "MSFT") for (ticker in tickers) { Data <- g

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-28 Thread David L Carlson
4 5 4 3 6 4 3 4 5 ... $ Transit: Date, format: "1985-10-01" "1985-11-01" ... Would be preferable. - David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 From: Paul Bernal [mailto:paulberna...

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-28 Thread Paul Bernal
-Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Paul > Bernal > Sent: Tuesday, March 28, 2017 9:12 AM > To: Ng Bo Lin > Cc: r-help@r-project.org > Subject: Re: [R] Looping Through DataFrames with Differing Lenghts > > Dear friends Ng Bo Li

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-28 Thread Ng Bo Lin
Hi Paul, Using the example provided by Ulrik, where > exdf1 <- data.frame(Date = c("1985-10-01", "1985-11-01", "1985-12-01”, > "1986-01-01"), Transits = c(NA, NA, NA, NA)) > exdf2 <- data.frame(Date = c("1985-10-01", "1986-01-01"), Transits = > c(15,20)), You could also try the following funct

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-28 Thread Ng Bo Lin
Hi Paul, The date format that you have supplied to R isn’t exactly right. Instead of supplying the format “%Y-%m-%d”, it appears that the format of your data adheres to the “%e-%B-%y” format. In this case, %e refers to Day, and takes an integer between (0 - 31), %B refers to the 3 letter abbrev

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-28 Thread David L Carlson
College Station, TX 77840-4352 -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Paul Bernal Sent: Tuesday, March 28, 2017 9:12 AM To: Ng Bo Lin Cc: r-help@r-project.org Subject: Re: [R] Looping Through DataFrames with Differing Lenghts Dear friends Ng Bo Lin, Mark and

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-28 Thread Paul Bernal
Dear Bo Lin, I tried doing Containerdata$TransitDate<-as.Date(Containerdata$TransitDate, "%e-%B-%y") but I keep getting NAs. I also tried a solution that I saw in stackoverflow doing: > lct<-Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C") [1] "C" > > Sys.setlocale("LC_TIME", lct) [1] "En

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-28 Thread Paul Bernal
Dear friends Ng Bo Lin, Mark and Ulrik, thank you all for your kind and valuable replies, I am trying to reformat a date as follows: Data<-read.csv("Container.csv") DataFrame<-data.frame(Data) DataFrame$TransitDate<-as.Date(DataFrame$TransitDate, "%Y-%m-%d") #trying to put it in -MM-DD for

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-28 Thread Ulrik Stervbo
Hi Paul, does this do what you want? exdf1 <- data.frame(Date = c("1985-10-01", "1985-11-01", "1985-12-01", "1986-01-01"), Transits = c(NA, NA, NA, NA)) exdf2 <- data.frame(Date = c("1985-10-01", "1986-01-01"), Transits = c(15, 20)) tmpdf <- subset(exdf1, !Date %in% exdf2$Date) rbind(exdf2, tmp

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-28 Thread Paul Bernal
Dear friend Mark, Great suggestion! Thank you for replying. I have two dataframes, dataframe1 and dataframe2. dataframe1 has two columns, one with the dates in -MM-DD format and the other colum with number of transits (all of which were set to NA values). dataframe1 starts in 1985-10-01 (oct

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-27 Thread Anthoni, Peter (IMK)
Hi Paul, match might help, but without a real data sample, it is hard to check if the following might work. mm=match(df.col378[,"Date"],df.col362[,"Date"]) #mm will have NAs, where there is no matching date in df.col362 #and have the index of the match, where the two dates match new.df=cbind(df.

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-27 Thread Ulrik Stervbo
You could use merge() or %in%. Best, Ulrik Mark Sharp schrieb am Mo., 27. März 2017, 22:20: > Make some small dataframes of just a few rows that illustrate the problem > structure. Make a third that has the result you want. You will get an > answer very quickly. Without a self-contained reprodu

Re: [R] Looping Through DataFrames with Differing Lenghts

2017-03-27 Thread Mark Sharp
Make some small dataframes of just a few rows that illustrate the problem structure. Make a third that has the result you want. You will get an answer very quickly. Without a self-contained reproducible problem, results vary. Mark R. Mark Sharp, Ph.D. msh...@txbiomed.org > On Mar 27, 2017,

Re: [R] looping problem

2017-02-02 Thread greg holly
help [mailto:r-help-boun...@r-project.org] On Behalf Of greg > holly > > Sent: Thursday, February 2, 2017 4:13 PM > > To: Rui Barradas > > Cc: r-help mailing list > > Subject: Re: [R] looping problem > > > > Thanks so much for this. Unfortunately, cbind did no

Re: [R] looping problem

2017-02-02 Thread PIKAL Petr
-40 which are basic for data input and manipulation. Cheers Petr > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of greg holly > Sent: Thursday, February 2, 2017 4:13 PM > To: Rui Barradas > Cc: r-help mailing list > Subject: Re

Re: [R] looping problem

2017-02-02 Thread greg holly
Thanks so much for this. Unfortunately, cbind did not work. Basically, I like to put an extra column named "chr" in the combined file from 22 chr. So chr colum will be "1" for the portion of chr1 in the combined file, 2 for the portion of chr2 in the combined file and so on. Regards, Greg On Thu

Re: [R] looping problem

2017-02-02 Thread Rui Barradas
Hello, If I understand correctly, just use ?cbind. Rui Barradas Em 02-02-2017 13:33, greg holly escreveu: Hi Rui; Is there any way to insert the chr ids in numeric as 1,2..,22 in the final output. Here is output from str(temp). So I need also chr ids in a column. 1 rs58108140 105

Re: [R] looping problem

2017-02-02 Thread greg holly
Hi Rui; Is there any way to insert the chr ids in numeric as 1,2..,22 in the final output. Here is output from str(temp). So I need also chr ids in a column. 1 rs58108140 10583 G A -0.070438 0.059903 2 rs189107123 10611 C G -0.044916 0.085853 Regards, Greg On Wed, Feb 1, 20

Re: [R] looping problem

2017-02-01 Thread greg holly
Hi Rui; I do appreciate for this. Thanks so much. I will try ASAP. Regards, Greg On Wed, Feb 1, 2017 at 1:32 PM, Rui Barradas wrote: > Hello, > > If what you want is to combine the files into one data.frame then there > are 2 things you should see: > > 1) You create a variable named 'temp' an

Re: [R] looping problem

2017-02-01 Thread Rui Barradas
Hello, If what you want is to combine the files into one data.frame then there are 2 things you should see: 1) You create a variable named 'temp' and don't ever use it. 2) You never combine the data.frames you read in. Try instead the following. temp <- data.frame() for(i in 1:22) { infi

Re: [R] Looping through data tables (or data frames) by removing previous individuals

2016-10-03 Thread Charles C. Berry
On Mon, 3 Oct 2016, Frank S. wrote: Dear R users, [deleted] I want to get a list of "k" data tables (or data frames) so that each contains those individuals who for the first time are at least 65, looping on each of the dates of vector "v". Let's consider the following example with 5 ind

Re: [R] Looping through data tables (or data frames) by removing previous individuals

2016-10-03 Thread Ista Zahn
Hi Frank, How about library(lubridate) dtf <- merge(dt, expand.grid(id = dt$id, refdate = v), by = "id") dtf[, gt65 := as.period(interval(fborn, refdate), unit = "years") > years(65)] dtf <- dtf[gt65 == TRUE,][, .SD[refdate == min(refdate)], by = id] Best, Ista On Mon, Oct 3, 2016 at 1:17 PM, F

Re: [R] Looping through different groups of variables in models

2016-09-01 Thread Kai Mx
Thanks so much everybody, especially to Dennis. I didn't really occur to me that I could put the models into a list. I have used dplyr for simple data transformations and will definitely look into it. On Thu, Sep 1, 2016 at 8:10 AM, Dennis Murphy wrote: > Hi: > > See inline. > > On Wed, Aug 31,

Re: [R] Looping through different groups of variables in models

2016-08-31 Thread Jim Lemon
Hi Kai, Perhaps something like this: kmdf<-data.frame(group=rep(c("exp","cont"),each=50), time=factor(rep(1:5,20)), condition=rep(rep(c("hot","cold"),each=25),2), value=sample(100:200,100)) for(timeindx in levels(kmdf$time)) { for(condindx in levels(kmdf$condition)) { cat("Time",timeindx,"Co

Re: [R] Looping through different groups of variables in models

2016-08-31 Thread Bert Gunter
Kai: 1. I think that this is a very bad idea, statistically, if I understand you correctly. Generally, your model should incorporate all groups, time points, and conditions together, not individually. 2. But plotting results in "small multiples" -- aka "trellis plots" may be useful. This is done

Re: [R] looping through rows of a logical matrix for combination with and testing of a numeric vector

2015-12-03 Thread debra ragland via R-help
Or sorry, I should clarify, I struggle with putting components together when it comes to looping. On Thursday, December 3, 2015 11:43 AM, debra ragland wrote: Thanks again! And no Bert, this is not homework. I have a very minimal background in R and struggle with putting concepts together.

Re: [R] looping through rows of a logical matrix for combination with and testing of a numeric vector

2015-12-03 Thread debra ragland via R-help
Thanks again! And no Bert, this is not homework. I have a very minimal background in R and struggle with putting concepts together. But thanks anyway. On Thursday, December 3, 2015 11:04 AM, Boris Steipe wrote: Use your logical vector to extract the x, y values for the test from the rows

Re: [R] looping through rows of a logical matrix for combination with and testing of a numeric vector

2015-12-03 Thread Boris Steipe
Use your logical vector to extract the x, y values for the test from the rows of the matrix: x <- mat[3, x2] y <- mat[3, !x2] Or: use the formula version of wilcox.test as explained in ?wilcox.test B. On Dec 3, 2015, at 10:28 AM, debra ragland via R-help wrote: > I have read in a seque

Re: [R] looping through rows of a logical matrix for combination with and testing of a numeric vector

2015-12-03 Thread Bert Gunter
I should have added -- is this homework? There is a no homework policy on this list. Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Thu, Dec 3, 2015 at 7:54 AM, Bert Gunter wrote: > Have you spent any time wit

Re: [R] Looping through multiple sub elements of a list to compare to multiple components of a vector

2015-12-02 Thread Adams, Jean
First, a couple posting tips. It's helpful to provide some example data people can work with. Also, please post in plain text (not html). If you have a single standard for comparison, you might find an approach like this helpful. # example data mylist <- c("AAEBCC", "AABDCC", "AABBCD") list.2 <

Re: [R] Looping help

2015-07-31 Thread Jim Lemon
Hi April, You need nested loops for something like this qs<- c(0,0.25,0.5,1,2,4,8,16,32,64) nrows<-dim(Data)[1] nqs<-length(qs) D.mat<-SE.mat<-matrix(NA,nrow=nrows,ncol=nqs) for(row in 1:nrows) { for(qval in 1:nqs) { # perform your calculation and set D.mat[row,qval] and SE.mat[row,qval] to the

Re: [R] Looping help

2015-07-30 Thread PIKAL Petr
Hi Your question is a bit cloudy. Simple loop can be realised to populate lists res<-vector(100, "list") for (i in 1:100) { lll <- do something based on i value res[[i]] <- put lll in ith place of the list } Cheers Petr > -Original Message- > From: R-help [mailto:r-help-boun...@r-pro

Re: [R] Looping Through List of .csv Files to Work with Subsets of the Data

2015-06-08 Thread William Dunlap
participant.files <- list.files("/Users/cdanyluck/Documents/Studies/MIG - Dissertation/Data & Syntax/MIG_RAW DATA & TXT Files/Plain Text Files") Try adding the argument full.names=TRUE to that call to list.files(). Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jun 8, 2015 at 7:15 PM, C

Re: [R] Looping Through List of .csv Files to Work with Subsets of the Data

2015-06-08 Thread Chad Danyluck
Thank you Don. I've incorporated your suggestions which have helped me to understand how loops work better than previously. However, the loop gets stuck trying to read the current file: mig.processed.data <- read.csv("/Users/cdanyluck/Documents/Studies/MIG - Dissertation/Data & Syntax/mig.log.dat

Re: [R] Looping Through List of .csv Files to Work with Subsets of the Data

2015-06-08 Thread MacQueen, Don
So you have 80 files, one for each participant? It appears that from each of the 80 files you want to extract three subsets of rows, one set for baseline one set for audio one set for "free" What I think I would do, if the above is correct, is create one "master" file. This file will have e

Re: [R] Looping and break

2015-03-02 Thread Rolf Turner
On 03/03/15 16:08, Jeff Newmiller wrote: Sigh. To be positive is to be wrong at the top of one's lungs. Next I will be told R has a goto statement. I am ***positive*** that it hasn't! :-) Well, 99.999% confident. Although I guess it's not inconceivable that some misguided nerd might constr

Re: [R] Looping and break

2015-03-02 Thread Jeff Newmiller
Sigh. To be positive is to be wrong at the top of one's lungs. Next I will be told R has a goto statement. --- Jeff NewmillerThe . . Go Live... DCN:Basics: ##.#. ##.#.

Re: [R] Looping and break

2015-03-02 Thread Rolf Turner
On 03/03/15 15:04, Jeff Newmiller wrote: Your example is decidedly not expressed in R, though it looks like you tried. Can you provide the hand-computed result that you are trying to obtain? Note that the reason you cannot find anything about next or break in R is that they don't exist. Point

Re: [R] Looping and break

2015-03-02 Thread Jeff Newmiller
Your example is decidedly not expressed in R, though it looks like you tried. Can you provide the hand-computed result that you are trying to obtain? Note that the reason you cannot find anything about next or break in R is that they don't exist. There are generally alternative ways to accomplis

Re: [R] looping multipanel plots to different figures

2015-02-19 Thread Jim Lemon
Hi efisio, Okay, you can switch devices using the dev.* functions in the grDevices package. If you only have two devices open at one time, this is not too difficult: #open both devices png(...) par(mfrow=c(1,2)) png(...) par(mfrow=c(1,2)) hist(...) dev.set(dev.next()) hist(...) dev.set(dev.next())

Re: [R] looping multipanel plots to different figures

2015-02-19 Thread efisio solazzo
Thanks Jim, actually I need to keep open two devices at the same time, and within the loop access either of them in alternation. In MatLab there is the command Figure(#) which keeps track of the open devices and direct the output of the plot to whichever of them. For example: plot_filenames<-c(

Re: [R] looping multipanel plots to different figures

2015-02-19 Thread Jim Lemon
Hi efisio, I read this as wanting to start a new graphics device, then set some plot parameters, display two plots and then close the graphics device at each iteration of the loop. If so, plot_filenames<-c("plot1.png","plot2.png","plot3.png") for(plotfn in plot_filenames) { png(plotfn) par(mfrow

Re: [R] looping in R

2014-04-02 Thread Duncan Murdoch
On 02/04/2014, 3:15 PM, Abugri James wrote: I ran the following loop on my SNP data and got an error message as indicated I would assume that the error message is accurate: as.character(current[1, "gene_old"]) has length zero. You'll need to debug why that happened. Duncan Murdoch for (i

Re: [R] looping in R

2014-04-02 Thread Jeff Newmiller
You desperately need to read the Posting Guide (mentioned in the footer of this email) which warns you not to post in HTML format, and learn how to make a reproducible example (e.g. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). The problem lies in some

Re: [R] looping through 3D array

2014-01-11 Thread arun
Hi Alex, Not sure if this is what you wanted. length(res) #from the previous 'example' using ##indx <- combn(dim(results)[1],2) #[1] 45 mat1 <- matrix(0,10,10)  mat1[lower.tri(mat1)] <- res  mat1[upper.tri(mat1)] <- res A.K. On Saturday, January 11, 2014 12:22 AM, alex padron wro

Re: [R] looping through 3D array

2014-01-10 Thread apadr007
It seems like emdist does not like to compare matrices with all 0 values. I ended up removing those from my 3D array and have ~8000 matrices instead of 13000. I am using res2 <- unlist(mclapply(seq_len(ncol(indx)),function(i) {x1 <- indx[,i]; emd2d(results[x1[1],,],results[x1[2],,]) }) ) But even

Re: [R] looping through 3D array

2014-01-09 Thread arun
Hi, No problem. You can use ?lower.tri() or ?upper.tri() res[lower.tri(res)] res[lower.tri(res,diag=TRUE)] #Other way would be to use: ?combn indx <- combn(dim(results)[1],m=2) res2 <- sapply(seq_len(ncol(indx)),function(i) {x1 <- indx[,i]; emd2d(results[x1[1],,],results[x1[2],,]) })  identical

Re: [R] looping through 3D array

2014-01-09 Thread arun
Hi, Try: library(emdist) set.seed(435) results<- array(sample(1:400,120,replace=TRUE),dim=c(10,3,4)) res <- sapply(seq(dim(results)[1]),function(i) {x1 <- results[i,,]; x2 <- results; sapply(seq(dim(x2)[1]),function(i) emd2d(x1,x2[i,,]))}) dim(res) #[1] 10 10 A.K. On Thursday, January 9, 2

Re: [R] looping function through list

2014-01-02 Thread arun
HI, I tested it on R 3.0.2 console (linux) and also on Rstudio Version 0.98.490.  It seems alright. A.K.  Thanks for this. I am trying to run the code you posted but Rstudio keeps crashing. I am trying to run it on the example output1 since it's small but that crashes as well. On Thursday

Re: [R] looping function through list

2014-01-02 Thread arun
#or  mapply(emd2d,sapply(output1,`[`,1),sapply(output1,`[`,2)) #[1]   NaN -6.089909 A.K. On Thursday, January 2, 2014 2:33 PM, arun wrote: Hi, May be this helps:  set.seed(42)  output1 <- list(list(matrix(0,8,11),matrix(0,8,11)), list(matrix(rnorm(80),8,10),matrix(rnorm(80),8,10)))  libra

Re: [R] looping function through list

2014-01-02 Thread arun
Hi, May be this helps:  set.seed(42)  output1 <- list(list(matrix(0,8,11),matrix(0,8,11)), list(matrix(rnorm(80),8,10),matrix(rnorm(80),8,10)))  library(emdist)  sapply(output1,function(x) {emd2d(x[[seq_along(x)[1]]],x[[seq_along(x)[2]]]) }) #[1]   NaN -6.089909 A.K. I'm trying to apply a fu

Re: [R] looping through columns in a matrix or data frame

2014-01-01 Thread arun
Hi, May be this helps: Using your function: mapply(less,test,4) #or  invisible(mapply(less,test,4)) #[1] 2 3 #[1] 3 #or  for(i in 1:ncol(test)){  less(test[,i],4)} #[1] 2 3 #[1] 3 A.K. Hi, I'm trying to figure out how to loop through columns in a matrix or data frame, but what I've been f

Re: [R] Looping over names of multiple data frames in an R for() loop XXXX

2013-10-13 Thread Mikkel Grum
You might want to try: assign(d[1], read.csv("yourfile.csv")) ... write.csv(d1, "yourfile.csv", append = FALSE) Regards Mikkel On Friday, October 11, 2013 2:53 PM, Dan Abner wrote: Hi everybody, I thought I was using the get() fn correctly here to loop over multiple data frame names in an

Re: [R] Looping over names of multiple data frames in an R for() loop XXXX

2013-10-11 Thread jim holtman
I think you want 'assign' at that point. Would suggest using a 'list' to store the input instead of unique named objects. 'list's are easier to manage. Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Fri

Re: [R] Looping an lapply linear regression function

2013-09-10 Thread arun
Hi, Try: dat2<- read.csv("BOlValues.csv",header=TRUE,sep="\t",row.names=1) dim(dat2) #[1] 20 28 indx2<-expand.grid(names(dat2),names(dat2),stringsAsFactors=FALSE) nrow(indx2) #[1] 784 indx2New<- indx2[indx2[,1]!=indx2[,2],] nrow(indx2New) #[1] 756 res2<-sapply(seq_len(nrow(indx2New)),function(i

Re: [R] Looping an lapply linear regression function

2013-09-06 Thread arun
HI, Using the example dataset (Test_data.csv): dat1<- read.csv("Test_data.csv",header=TRUE,sep="\t",row.names=1) indx2<-expand.grid(names(dat1),names(dat1),stringsAsFactors=FALSE) indx2New<- indx2[indx2[,1]!=indx2[,2],] res2<-t(sapply(seq_len(nrow(indx2New)),function(i) {x1<- indx2New[i,]; x2<-c

Re: [R] Looping an lapply linear regression function

2013-09-05 Thread arun
HI, May be this helps:  set.seed(28)  dat1<- setNames(as.data.frame(matrix(sample(1:40,10*5,replace=TRUE),ncol=5)),letters[1:5]) indx<-as.data.frame(combn(names(dat1),2),stringsAsFactors=FALSE) res<-t(sapply(indx,function(x) {x1<-cbind(dat1[x[1]],dat1[x[2]]);summary(lm(x1[,1]~x1[,2]))$coef[,4]}))

Re: [R] Looping an lapply linear regression function

2013-09-05 Thread Flavio Barros
Hello Arun. Can you provide some data? To help you better i will need a complete reproducible example ok? On Thu, Sep 5, 2013 at 1:49 PM, arun wrote: > HI, > May be this helps: > set.seed(28) > dat1<- > setNames(as.data.frame(matrix(sample(1:40,10*5,replace=TRUE),ncol=5)),letters[1:5]) > indx

Re: [R] Looping in Matrix

2013-06-24 Thread David Winsemius
On Jun 24, 2013, at 12:13 AM, Fazli Raziq wrote: > Hello all, > > I want to construct "Two way Matrix". The Algorithm is like: > > 1) Data of time with censoring or events > 2) Predictor variables (genes) > 3) Resample original data in step 1 and 2 by WR > 4) Apply Coxph Model to resample d

Re: [R] Looping Over Data Frames

2013-04-29 Thread David Winsemius
On Apr 29, 2013, at 6:03 PM, Sparks, John James wrote: > Dear R Helpers, > > I am re-phrasing a question that I put forth earlier today due to some > particulars in the solution that I am searching for. Many thanks to those > who answered the previous post and to any who would be willing to ans

Re: [R] Looping through names of both dataframes and column-names

2013-04-26 Thread Daniel Egan
Much thanks Blaser. That worked perfectly. This will improve my code considerably. Greatly appreciated. Regards, Dan On Fri, Apr 26, 2013 at 3:48 AM, Blaser Nello wrote: > Here are two possible ways to do it: > > This would simplify your code a bit. But it changes the names of x_cs to > cs.x.

Re: [R] Looping through names of both dataframes and column-names

2013-04-26 Thread Blaser Nello
Here are two possible ways to do it: This would simplify your code a bit. But it changes the names of x_cs to cs.x. for (df in nls) { assign(df, cbind(get(df), cs=apply(get(df), 2, cumsum))) } This is closer to what you have done. for (df in nls) { print(df) for (var in names(get(df)))

Re: [R] Looping through rows of all elements of a list that has variable length

2013-02-04 Thread Dimitri Liakhovitski
Thanks a lot, Pratap! Dimitri On Mon, Feb 4, 2013 at 9:43 AM, nalluri pratap wrote: > Just modified your code a bit, hope this helps: > > a=expand.grid(1:2,1:2) > b=expand.grid(1:2,1:2,1:2) > c=expand.grid(1:2,1:2,1:2,1:2) > l.long<-list(a,b,c) > mygrid<-do.call(expand.grid,lapply(l.long,funct

Re: [R] Looping through rows of all elements of a list that has variable length

2013-02-04 Thread nalluri pratap
Just modified your code a bit, hope this helps:   a=expand.grid(1:2,1:2) b=expand.grid(1:2,1:2,1:2) c=expand.grid(1:2,1:2,1:2,1:2) l.long<-list(a,b,c) mygrid<-do.call(expand.grid,lapply(l.long,function(x) 1:nrow(x))) out<-vector("list",nrow(mygrid)) for(gridrow in 1:nrow(mygrid)) {   sum_rows=0

Re: [R] looping through spatial points and getting counts of spatial points in spatial grid in R

2012-12-18 Thread Roger Bivand
Tilottama Ghosh yahoo.com> writes: > > Hi, > Following Anthony's reply I am attaching two of the POI shapefiles and the code for creating the spatial > grid. I am not being able to read the list of these shapefiles and then perform the points in spatial grid > function. If someone can help, I wi

Re: [R] looping through spatial points and getting counts of spatial points in spatial grid in R

2012-12-17 Thread Tilottama Ghosh
Hi, Following Anthony's reply I am attaching two of the POI shapefiles and the code for creating the spatial grid. I am not being able to read the list of these shapefiles and then perform the points in spatial grid function. If someone can help, I will be very grateful. Here is my code for crea

Re: [R] looping through spatial points and getting counts of spatial points in spatial grid in R

2012-12-17 Thread John Kane
Sample data? Please supply some sample data and code. The easiest way to supply data is to use the dput() function. Example with your file named "testfile": dput(testfile) Then copy the output and paste into your email. For large data sets, you can just supply a representative sample.

Re: [R] Looping matrix multiplication

2012-11-08 Thread R. Michael Weylandt
On Thu, Nov 8, 2012 at 6:16 PM, Nordlund, Dan (DSHS/RDA) wrote: > >> -Original Message- >> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- >> project.org] On Behalf Of wilkesma >> Sent: Thursday, November 08, 2012 5:49 AM >> To: r-help@r-project.org >> Subject: [R] Looping mat

Re: [R] Looping matrix multiplication

2012-11-08 Thread Nordlund, Dan (DSHS/RDA)
> -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of wilkesma > Sent: Thursday, November 08, 2012 5:49 AM > To: r-help@r-project.org > Subject: [R] Looping matrix multiplication > > I am dealing with a number of time-series consis

Re: [R] Looping survdiff

2012-10-19 Thread Charles Determan Jr
Thank you for all your responses, I assure you this is not homework. I am a graduate student and my classes are complete. I am trying multiple different ways to analyze data and my lab requests different types of scripts to accomplish various tasks. I am the most computer savy in the lab so it c

Re: [R] Looping survdiff

2012-10-19 Thread Terry Therneau
The number of recent questions from umn.edu makes me wonder if there's homework involved Simpler for your example is to use get and subset. dat <- structure(.as found below var.to.test <- names(dat)[4:6] #variables of interest nvar <- length(var.to.test) chisq <- double(nvar) for (

Re: [R] looping survdiff?

2012-10-19 Thread Andrews, Chris
> sapply(seq(4,ncol(dat)), function(i) > survdiff(Surv(time,completion=="2")~dat[,i], data=dat, > subset=group=="3")$chisq) [1] 0.0944 4.4854 3.4990 Chris -Original Message- From: Charles Determan Jr [mailto:deter...@umn.edu] Sent: Thursday, October 18, 2012 3:04 PM To: r-help@r-projec

Re: [R] looping through numbered variables

2012-08-27 Thread Daniel Caro
Thank you, character indexing (?"[") is what I was looking for: sapply(paste('var', 1:100, sep=''), function(x) weighted.mean(data[[x]], data$weight)) Daniel On Mon, Aug 27, 2012 at 6:26 PM, Bert Gunter wrote: > Have you read "An Introduction to R". If not, please do so before > osting and pay

Re: [R] looping through numbered variables

2012-08-27 Thread David Winsemius
On Aug 27, 2012, at 7:11 AM, Daniel Caro wrote: Hello, This is a beginner question. You should read the Posting Guide. (As well as following Bert's suggestion to work through "Intro to R". I am trying to loop through numbered variables with "apply" to calculate weighted means. My data i

Re: [R] looping through numbered variables

2012-08-27 Thread Bert Gunter
Have you read "An Introduction to R". If not, please do so before osting and pay particular attention to the section on indexing. If so, re-read the sections on indexing. For a terser exposition, ?"[" -- Bert On Mon, Aug 27, 2012 at 7:11 AM, Daniel Caro wrote: > Hello, > > This is a beginner qu

  1   2   >