[R] Creating a loop with code from the mblm package

Bert Gunter bgunter.4567 at gmail.com
Sun Sep 4 16:58:47 CEST 2016


Please go through an R tutorial or two before posting further here.
There are many good ones on the web. Some recommendations can be found
here:
https://www.rstudio.com/online-learning/

Your question:

"And my Year column is the first column in my csv file, which I
thought made it column 0. Am I mistaken, is it supposed to be column
1?"

is absolutely basic, and indicates that you have not yet made much
effort to learn R. The inevitable result, of course, is confusion and
errors of the sort you describe.

The answer is that R indices start at 1, but there is a great deal
more to them than that, which tutorials would tell you about.

Cheers,
Bert




Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Sep 4, 2016 at 7:10 AM, Bailey Hewitt <bailster at hotmail.com> wrote:
> Hi Jim,
>
>
> Thank you for the suggestion. Unfortunately, when I tried this it gave me the same error as I was getting before. I was wondering the same thing as you, because of the way my function is set up is it even possible to iterate through columns in that type of function? And my Year column is the first column in my csv file, which I thought made it column 0. Am I mistaken, is it supposed to be column 1?
>
>
> Thanks!
>
>
> Bailey
>
>
> ________________________________
> From: Jim Lemon <drjimlemon at gmail.com>
> Sent: September 4, 2016 4:55 AM
> To: Bailey Hewitt
> Cc: R-help
> Subject: Re: [R] Creating a loop with code from the mblm package
>
> Hi Bailey,
> Treat it as a guess, but try this:
>
> for (i in c(1:3)){
>  y<-mydata[,i]
>  x <- mblm(y ~ Year, mydata, repeated = FALSE)
>  print(x)
> }
>
> I'm not sure that you can mix indexed columns with column names. Also,
> Year is column 4, no?
>
> Jim
>
>
> On Sun, Sep 4, 2016 at 11:43 AM, Bailey Hewitt <bailster at hotmail.com> wrote:
>> Hello,
>>
>>
>> I am a novice in coding in R and have come across an error I am having a hard time fixing. I am trying to use the mblm package to run a Theil-Sen linear model. The code for this function is:
>>
>> mblm(Y ~ X, dataframe, repeated = FALSE)
>>
>> My goal is to put this into a loop so that I can calculate the Theil-Sen slope of each column in my csv. file. The file contains one column of years (x value) and 3 columns of days of the year (y values). All columns are the same length. The code I currently have is:
>>
>>
>> read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = TRUE, sep = ",")
>>
>> mydata= read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = TRUE, sep = ",")
>>
>>
>> attach(mydata)
>>
>>
>> install.packages("mblm")
>>
>> library("mblm")
>>
>>
>> for (i in c(1:3)){
>>
>>   x <- mblm(mydata[,i] ~ Year, mydata, repeated = FALSE)
>>
>>   print(x)
>>
>> }
>>
>>
>> Which gives me the following error:
>>
>> Error in names(res$residuals) = as.character(1:length(res$residuals)) :
>>
>>   'names' attribute [2] must be the same length as the vector [0]
>>
>>
>> Which I cannot seem to solve although as I understand it it is an error that I am causing in the mblm package. If anyone has any insight into how I [[elided Hotmail spam]]
>>
>>
>> Bailey
>>
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>
> z.ch/mailman/listinfo/r-help>
> stat.ethz.ch
> The main R mailing list, for announcements about the development of R and the availability of new code, questions and answers about problems and solutions using R ...
>
>
>
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list