我需要帮助在R中编写嵌套的for循环

时间:2019-07-14 19:43:26

标签: r

在文字上很难解释,但请尝试。直到出现“直到这里都很好”为止。因此,我有一个数据集,其范围介于5到9年之间,并与许多量表相关。第31行上的第一个for循环使得所有间隔都是一年,现在我正在尝试分配间隔内所有年份的标度数。我无法为该部分使用嵌套的for循环

我尝试了for循环的多种变体,但不确定如何在r中进行。我有一个成功的成功的Matlab代码

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]
           }}
     }
 }

我的数据如下第1列:c(1922、1913、1905、1896、1888、1879、1871、1862、1855、1847、1840) 列2:c(1、6、3、0、6、0、2、0、8、5、1.333)

我希望得到矩阵读数(第一个数字是第1列-第二个数字s第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,等等

我希望得到一个间隔为1年的矩阵,每个间隔具有相同数量的量表。即1922-1915 = 6磅秤->输出= 1922-6磅秤,1921-6磅秤,1920-6磅秤,等等。

1 个答案:

答案 0 :(得分:0)

我不确定您的问题是否可以理解,但是您可以尝试以下方法:

# 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)


相关问题