Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:01:54+00:00 2026-06-14T05:01:54+00:00

I’m trying to fill an empty matrix with results calculated from a for loop.

  • 0

I’m trying to fill an empty matrix with results calculated from a for loop. I don’t know how to specify the indexing in my for loop i.e., right now my for loop code does not work and I can’t get result into my matrix.

Here’s a subset of my data:

> dput(eg)
structure(list(month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("7", 
"8", "9", "10", "11"), class = "factor"), LONG = c(980.744064583783, 
983.838644208237, 978.459941419921, 984.841644157878, 968.200358699171, 
967.552217901586, 981.077638048121, 975.435563330951, 982.123246960536, 
975.383981047645, 978.181377755365, 975.001205420472, 980.410452250835, 
971.112535576725, 980.744064583783, 981.789215313314, 982.835292295612, 
975.435563330951, 982.457239633824, 976.095788761194, 977.468633236679, 
983.50423264086, 985.221850881562, 983.838644208237, 980.410452250835, 
970.785922305654, 983.838644208237, 981.789215313314, 975.435563330951, 
968.457651588967, 981.744668260738, 976.807567823793, 975.765695304413, 
977.850367309317, 967.487216377624, 967.487216377624, 981.121035207435, 
970.785922305654, 981.455144719747, 976.372538872237, 947.785924542022, 
949.961077537064, 953.277469897636, 956.588193000174, 954.107424886471, 
969.362874319781, 967.61505086351, 986.40950486552, 989.671406838413, 
914.2401235585), LAT = c(7497.04118692311, 7488.13522468594, 
7485.26645510239, 7482.63600422376, 7489.1136371139, 7492.78277930456, 
7495.20802780153, 7497.97375736711, 7493.50450717453, 7494.17837952228, 
7490.89595766574, 7492.21670554412, 7498.87433550932, 7493.41424827727, 
7497.04118692311, 7495.33760219375, 7493.63434928842, 7497.97375736711, 
7491.67140156528, 7494.30641015495, 7490.76739320858, 7489.96827686286, 
7484.59949728804, 7488.13522468594, 7498.87433550932, 7495.2484410159, 
7488.13522468594, 7495.33760219375, 7497.97375736711, 7483.48377437677, 
7491.5416779275, 7494.4346337625, 7496.14008888173, 7492.72950917327, 
7488.98777760417, 7488.98777760417, 7499.00376048698, 7495.2484410159, 
7497.17068662916, 7488.67706076623, 7444.0453546661, 7444.40653631031, 
7446.84930605798, 7449.2962767147, 7450.76636540362, 7494.995420297, 
7496.57764523632, 7465.87604399919, 7468.36476722201, 7434.9359208897
)), .Names = c("month", "LONG", "LAT"), row.names = c(740L, 741L, 
742L, 743L, 751L, 762L, 773L, 784L, 795L, 806L, 817L, 828L, 744L, 
745L, 746L, 747L, 748L, 749L, 750L, 752L, 753L, 754L, 755L, 756L, 
757L, 758L, 759L, 760L, 761L, 763L, 764L, 765L, 766L, 767L, 768L, 
769L, 770L, 771L, 772L, 774L, 775L, 776L, 777L, 778L, 779L, 780L, 
781L, 782L, 783L, 785L), class = "data.frame")

This is what I’ve done so far:

# CREATE AN EMPRTY MATRIX FOR RESULTS
    nrow<-3 #there are 3 results calculated for each ncol
    ncol<-length(unique(eg$month))
    total<-matrix(0,nrow=nrow,ncol=ncol)

    month<-unique(eg$month)
    library(spatstat) #need spatstat for calculating the window & nndist
    W2<-ripras(eg$LONG,eg$LAT) #defining the window

    # FOR LOOP TO CALCULATE NNDIST < & > 1KM & == 0KM FOR EACH MONTHLY POINT PATTERN 

    for(i in month){
      m<-subset(eg, month==i) #subsetting my data for each month
      mp<-ppp(m$LONG,m$LAT,window=W2) #creating a point pattern
      nnd<-nndist(mp) #calculating the nearest neighbour in a point pattern
      total[1,i]<-length(nnd[which(nnd>1)]) #Filling up the matrix with results 1
      total[2,i]<-length(nnd[which(nnd==0)]) 
      total[3,i]<-length(nnd[which(nnd<1)])
    }

I get this error message:

Error in total[1, i] <- length(nnd[which(nnd > 1)]) : 
  no 'dimnames' attribute for array

EDITS:

Thanks @SvenHohenstein, I need to use either for(i in seq_along(month)) or for (i in 1:length(month)), however the problem is that my months are factors with levels “7” and “8”.

I have to first rename my factors level before running the loop (which now works!). However is there another way of doing this without renaming your factors?

levels(eg$month)<-c(1:2) 
for(i in 1:length(month)){
  m<-subset(DF, month==i) #subsetting my data for each month
  mp<-ppp(m$LONG,m$LAT,window=W2) #creating a point pattern
  nnd<-nndist(mp) #calculating the nearest neighbour in a point pattern
  total[1,i]<-length(nnd[nnd>1]) #Filling up the matrix with results 1
  total[2,i]<-length(nnd[nnd==0]) 
  total[3,i]<-length(nnd[nnd<1])
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:01:57+00:00Added an answer on June 14, 2026 at 5:01 am

    This should work:

    for(i in 1:length(month)){
      mo <- month[i]
      m<-subset(eg, month==mo) #subsetting my data for each month
      mp<-ppp(m$LONG,m$LAT,window=W2) #creating a point pattern
      nnd<-nndist(mp) #calculating the nearest neighbour in a point pattern
      total[1,i]<-length(nnd[nnd>1]) #Filling up the matrix with results 1
      total[2,i]<-length(nnd[nnd==0]) 
      total[3,i]<-length(nnd[nnd<1])
    }
    

    As an alternative (without for loops):

    library(spatstat)
    W2 <- ripras(eg$LONG, eg$LAT)
    
    dat <- setNames(rep(0, 3), c(1, 0, -1))
    
    do.call(cbind,
            lapply(split(eg, eg$month, drop = TRUE),
                   function(m) {
                     tab <- rev(table(sign(nndist(ppp(m$LONG, m$LAT, window = W2)))))
                     dat[match(names(tab), names(dat))] <- tab
                     return(dat)
                   }))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.