0
votes

its super hard to explain in text but ill try. it works up until where it says 'good until here'. so i have a data set with years that range from 5-9 year intervals associated with a number of scales. the first for loop on line 31 makes it so all of the intervals are one years and now i am trying to assign the number of scales for all of the years within the interval. I cannot get the nested for loops to work for that part

I have tried multiple variations of the for loops but am unsure how to do it in r. I have a successful matlab code that worked

remove(years)
remove(yearly_data, year_new_data)

num_intervals = nrow(Sard1301)                     #counts the numbers of intervals in the dataset
number_intervals = data.frame(interval = 1:num_intervals)

first_year = (Sard1301[num_intervals,1])           #Specifies the youngest/earliest year in the dataset (on the bottom of the dataset)

last_year_first_interval = (Sard1301[1, 1])        #Specified the first interval of the oldest year (top of the dataset)

end_samp_interval = Sard1301[1,1]-Sard1301[2,1]    #Specifies the interval between the most recent and second most recent intervals

last_year = round(Sard1301[1,1]+end_samp_interval)        #Specifies the second interval in the last sampling interval (i.e. the most recent year plus the interval)

total_years = (last_year-first_year)
num_years = as.numeric(last_year-first_year)            #Specifies the total number of years and converts it to a numeric value (from a data.frame)


yearly_data = round(matrix(data = 0, num_years, ncol=2))     #This creates a blank matrix for the data to be added to
years=yearly_data[ ,1]

##################################
#yearly_data1 = rbind(yearly_data[1,1], last_year)     ##########
#yearly_data[1,1] = as.numeric(last_year)                    #This makes the first row in the first column the most recent year (second part of the interval)
years[1] = as.numeric(round(last_year))  

#for (i in total_years) {
#yearly_data[i+1,1]=yearly_data[i,1]-1  
#}

num_steps=length(years)-1
counter=1
for(i in 1:num_steps) {
  years[counter+1]= years[counter]-1
  counter=counter+1
}
yearly_data[,1]=years

#yearly_data1 = rbind(yearly_data[1,1], last_year)    #good up until here


num.int = length(num_intervals)

  for(j in 1:num_int){
    for(k in 1:num_steps){
      year_new_data = yearly_data#[k, 1]

      if (j==1){
        if(year_new_data>Sard1301){#[j,1]) {
          yearly_data=Sard1301#[j,2]
          }}
        else((j>1) & (j<num_intervals-1))
          if(year_new_data>=round(Sard1301) & year_new_data<round(Sard1301)) {
            yearly_data=Sard1301
        }
       if(j==num.int) {
           if(year_new_data<=Sard1301) {
             yearly_data[2] = Sard1301[2]
           }}
     }
 }

my data is as follows column 1: c(1922, 1913, 1905, 1896, 1888, 1879, 1871, 1862, 1855, 1847, 1840) column 2: c(1, 6, 3, 0, 6, 0,2,0,8, 5, 1.333)

I hope to get a matrix reading (the first number is column 1 - second number s column 2) 1922 - 1, 1921, 1, 1920 - 1, 1919 - 1, 1918 -1, 1917 - 1, 1916 - 1, 1915 - 1, 1914 - 1, 1913 - 6, 1912 - 6, 1911 - 6, 1910 - 6, 1909 - 6, 1908 - 6, 1907 - 6, 1906 - 6, 1905 - 3, 1904 - 3, etc.

I expect to get a matrix with 1 year intervals that have the same number of scales for each of the intervals. i.e 1922-1915 = 6 scales---> output = 1922-6 scales, 1921 - 6 scales, 1920 - 6 scales, etc.

1
Do you need help writing for loops, or do you need help solving your problem? Loops have their place in R code, but more often than not the optimal solution is either vectorized or uses the implicit looping abstracted in functions in the apply family.John Coleman
It'd be easier to help if you just share a small subset of your data, briefly describe the calculation, and show expected output.Shree
John- what does the apply function do?Alex Filardo
Shree- I added what my dataset looks like and what i expect to seeAlex Filardo
See How to make a great R reproducible example and update your question accordingly. It is very hard to understand in it's current form.Shree

1 Answers

0
votes

I'm not sure to understand your problem but you can try this:

# 2 columns
x1<-c(1922, 1913, 1905, 1896, 1888, 1879, 1871, 1862, 1855, 1847, 1840)
x2<-c(1, 6, 3, 0, 6, 0,2,0,8, 5, 1.333) 



# order data
a1<-order(x1)
x1<-x1[a1]
x2<-x2[a1]

# create two vectors
sc<-fromto<-min(x1):max(x1)
for(i in 1:length(x1)){
  if(i == 1){
   d<-which(fromto<=x1[i])
   sc[d]<-x2[i]
  }else{
    d<-which(fromto<=x1[i] & fromto>=x1[i-1])
    sc[d]<-x2[i]
  }
}
finalmatrix<-cbind(fromto,sc)

# matrix
print(finalmatrix)