# R error when I try to filter data and apply sample weights

**URL:** <https://forum.ipums.org/t/r-error-when-i-try-to-filter-data-and-apply-sample-weights/3707>\
**Category:** CPS\
**Created:** [August 20, 2020, 11:57pm UTC](https://forum.ipums.org/t/r-error-when-i-try-to-filter-data-and-apply-sample-weights/3707 "2020-08-20T23:57:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ray\_Khalfani](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.ipums.org/ray_khalfani/32/381_2.png) [@Ray\_Khalfani](https://forum.ipums.org/u/Ray_Khalfani)\
**Post date:** [August 20, 2020, 11:57pm UTC](https://forum.ipums.org/t/r-error-when-i-try-to-filter-data-and-apply-sample-weights/3707/1 "2020-08-20T23:57:21Z")

</div>

I get the following error:

Error in proxy[i, …, drop = FALSE] : incorrect number of dimensions

Please help. I can’t seem to figure out where I’m going wrong. Is my For Loop wrong? Or is my created variable named “output” being done incorrectly? Everything seems right until I get to my For Loop. Here’s my code:

ddi ← read\_ipums\_ddi(“cps\_00026.xml”)  
data ← read\_ipums\_micro(ddi)  
data2 ← data%\>%  
rename\_all(tolower)

data3 ← data2%\>%  
mutate(nh\_white = if\_else(race==100 & hispan==0, 1, 0),  
nh\_black = if\_else(race==200 & hispan==0, 1, 0),  
nh\_api = if\_else(race %in% c(651, 652) & hispan==0, 1, 0),  
nh\_other = if\_else(!race %in% c(100, 200, 651, 652) & hispan==0, 1, 0),  
latinx = if\_else(hispan \> 0 & hispan \< 900, 1, 0),  
wtfinl\_six\_mo = wtfinl/6  
)  
years ← c(2014,2015,2016,2017,2018,2019)  
output ← vector(“double”, length(years))  
for (i in seq\_along(years)) {  
data\_filtered ← data3 %\>%  
#Filter for all Georgia entries where the source of public assistance income  
#is not coded as not in Universe (0)  
filter(srcwelfr == 1 & srcwelfr == 2 & srcwelfr == 3 &  
year==years[i] & statefip == 13)  
#Apply proper weights  
table1 ← wtd.table(data\_filtered$srcwelfr, weights = data\_filtered$asecwth)  
table1

# Turn that table into a dataframe so that we can work with it

srcwelfr\_year ← as.data.frame(table1)  
srcwelfr\_year ← reshape(srcwelfr\_year, idvar = “status”, timevar = “Var1”, direction = “wide”)  
print(srcwelfr\_year)

---

<div class="post-metadata">

**Author:** ![Grace\_Cooper](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.ipums.org/grace_cooper/32/376_2.png) [@Grace\_Cooper](https://forum.ipums.org/u/Grace_Cooper)\
**Post date:** [August 25, 2020, 6:16pm UTC](https://forum.ipums.org/t/r-error-when-i-try-to-filter-data-and-apply-sample-weights/3707/2 "2020-08-25T18:16:04Z")

</div>

I recreated your code and was able to run the for loop without error, though when I tried to execute creation of the table using the data frame created by the for loop, the error came up. The error message “Error in proxy[i, …,drop = FALSE] : incorrect number of dimensions” is coming up because the for loop you have written results in an empty dataframe. I can’t tell completely what your end goal is based on your code, but I think it may be unnecessary to use a for loop to get your desired result. For sample code and examples of using IPUMS data in R, you may be interested in these [exercises](https://ipums.org/support/exercises). I don’t know what is appropriate for your specific analytical approach, but think you could leverage group\_by() and summarize() to accomplish what you are doing in the loop you shared in your initial post. Also, keep in mind that if you are summarizing person-level data (such as [SRCWELFR](https://cps.ipums.org/cps-action/variables/SRCWELFR#codes_section)), you need to use the person-level weight, [ASECWT](https://cps.ipums.org/cps-action/variables/ASECWT#codes_section).

---

<div class="post-metadata">

**Author:** ![Ray\_Khalfani](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.ipums.org/ray_khalfani/32/381_2.png) [@Ray\_Khalfani](https://forum.ipums.org/u/Ray_Khalfani)\
**Post date:** [August 25, 2020, 6:28pm UTC](https://forum.ipums.org/t/r-error-when-i-try-to-filter-data-and-apply-sample-weights/3707/3 "2020-08-25T18:28:33Z")

</div>

Much thanks for your response. Regarding the not so necessary need for a For Loop, you’re right. After much trial and error, I changed my analysis. Here’s a snippet of it, using group\_by and summarize instead. (As I’m still working on gaining more savvy with R, at some point I must learn how to loop this code across multiple years, so I don’t have to manually write out this method of analysis for each year that I’m researching.)

ddi ← read\_ipums\_ddi(“cps\_00028.xml”)  
data ← read\_ipums\_micro(ddi)  
data2 ← data%\>%  
rename\_all(tolower)  
data3 ← data2%\>%  
mutate(nh\_white = if\_else(race==100 & hispan==0, 1, 0),  
nh\_black = if\_else(race==200 & hispan==0, 1, 0),  
nh\_api = if\_else(race %in% c(651, 652) & hispan==0, 1, 0),  
nh\_other = if\_else(!race %in% c(100, 200, 651, 652) & hispan==0, 1, 0),  
latinx = if\_else(hispan \> 0 & hispan \< 900, 1, 0),  
wtfinl\_six\_mo = wtfinl/6  
)  
##############################################################################

#DOING 2014 ANALYSIS  
year2014 ← data3%\>%  
filter(year==2014)

#2014 Question1: By race, ethnicity and gender, what are the counts and  
#proportions of TANF recipients in GA with an SPM unit status  
#ABOVE poverty?  
year2014analysis1 ← year2014%\>%  
filter(srcwelfr == 1, spmpov == 0, statefip==13)%\>%  
group\_by(hispan = haven::as\_factor(hispan), race = haven::as\_factor(race),  
sex = haven::as\_factor(sex))%\>%  
summarize(n = sum(asecwt))%\>%  
mutate(pct = n/sum(n))
