LISTSERV mailing list manager LISTSERV 16.0

Help for R-USERS-L Archives


R-USERS-L Archives

R-USERS-L Archives


R-USERS-L@LISTS.UFL.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

R-USERS-L Home

R-USERS-L Home

R-USERS-L  2018

R-USERS-L 2018

Subject:

How to fill up a column using certain criterias

From:

"Cardoso Carrero,Gabriel" <[log in to unmask]>

Reply-To:

UF R Users List <[log in to unmask]>

Date:

Tue, 19 Jun 2018 20:24:53 +0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (1 lines)

Good afternoon everybody

I’m having a hard time to fill up a column with the sum of certain values of other columns.

…..

The table has over 19,000 lines, because each YEAR I have a proportion deforested in a given LOT.

I created a new column with Cumulative_def_proportion, which I want to fill up with the SUM of cumulative  deforestation proportion of i lot for a certain i YEAR.

Example:
 ID_LOT YEAR Proportion_DEF cohort  lot_year ID_HH A_DEF_PERIOD
1          1 2015           0.47   2015    1_2015    89       55.473
2          1 2016           0.19   2015    1_2016    89       22.334
3          2 2015           0.71   2015    2_2015    89       69.181
4          2 2016           0.11   2015    2_2016    89       10.202

What I want is that in a new column (Cum_prop), in line 2 would appear the cumulative proportion of deforestation of lot 1 up to 2016, which is 0.66, and in line 4 it would be 0.82 for lot 2.

I have in total 13 YEARs and lot cohort could be any of those 13 years.

The looping is running, but it returns the Cum_prop column empty=NA

BELOW IS THE SCRIPT I GOT SO FAR…I tried to recycle one script I had, but not sure if it is the same rationale or if I would have a simpler way to do that.

I think the problem is in filling the table because I’m not sure I am using %in% correctly or is.na


##########

dat= read.csv("Dados_final_cohortXY_prop.csv")
dat$Cum_prop= NA

v.years = sort(unique(dat$YEAR))
names(dat)
system.time( #calculates time used
  for(i in 1:length(v.years)){

    t.year = v.years[i] #selects year "i"

    t.table = dat[dat$YEAR <= t.year, ] #creates a temporary table only with data up to year "i"

    t.sum.lot = aggregate(list(prop.sum = t.table$Proportion_DEF), list(ID_LOT = t.table$ID_LOT), sum) #sum proportions of def per ID_LOT

    prop.sum = t.sum.lot$prop.sum
    length(t.sum.lot) #checking


    #fill table
    # where vector Cum_prop is NA and with lot within do vector “t.sum.lot" it will be the sum of sum.prop
    dat$Cum_prop[dat$ID_LOT %in% t.sum.lot &
                   is.na(dat$Cum_prop)] = prop.sum

    #remove objetos temporarios
    rm(t.year, t.table, t.sum.lot)

    #mostra o decorrimento do looping
    print(paste(i, "/", length(v.years)))

  } #end looping "i"
) #end system.time

##########

Thank you very much in advance
Gabriel

Gabriel C. Carrero
PhD Student
Department of Geography
University of Florida
(352) 888-0234
P.O. Box 117315



Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

2019
2018
2017
2016
2015
2014
2013
2012
2011
2010
2009
2008

ATOM RSS1 RSS2



LISTS.UFL.EDU

CataList Email List Search Powered by the LISTSERV Email List Manager