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 1013315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:04:56+00:00 2026-05-16T10:04:56+00:00

I have the following data read into R as a data frame named data_old:

  • 0

I have the following data read into R as a data frame named “data_old”:

   yes year month
1  15 2004     5
2   9 2005     6
3  15 2006     3
4  12 2004     5
5  14 2005     1
6  15 2006     7
.   .  ...     .
.   .  ...     .

I have written a small loop which goes through the data and sums up the yes variable for each month/year combination:

year_f <- c(2004:2006)
month_f <- c(1:12)

for (i in year_f){
    for (j in month_f){
        x <- subset(data_old, month == j & year == i, select="yes")
        if (nrow(x) > 0){
            print(sum(x))
            }
        else{print("Nothing")}
        }
    }

My question is this: I can print the sum for each month/year combination in the terminal, but how do i store it in a vector? (the nested loop is giving me headaches trying to figure this out).

Thomas

  • 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-05-16T10:04:56+00:00Added an answer on May 16, 2026 at 10:04 am

    Another way,

    library(plyr)
    ddply(data_old,.(year,month),function(x) sum(x[1]))
    
      year month V1
    1 2004     5 27
    2 2005     1 14
    3 2005     6  9
    4 2006     3 15
    5 2006     7 15
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following question. I'd like to place a file named data.xml into sdcard/appname
I have the following code: <?php $FILE=giant-data-barf.txt; $fp = fopen($FILE,'r'); //read everything into data
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I have following code to serialize my data into a file: out = new
I have the following data-structure as a class named Task: private: string name; int
Hi there I have to read data from text file into the array in
I have a comma separated file named foo.csv containing the following data: scale, serial,
I have the following code to read data from my .plist for use in
I have the following data frame: Date1 Date2 Date3 Date4 Date5 1 25 April
I have following data to save in database using php my data is :

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.