I'm trying to create a very basic function in which I want to remove any 0 values (or less) from the df based on a specific column in the df. When I run these lines outside of the function they work but when I try to run them within the function I get this error "Error in $<-.data.frame
(*tmp*
, name, value = numeric(0)) : replacement has 0 rows". Does anyone know what the problem is?
Remove_Missing=function(x,name){
x$name=as.numeric(x$name)
x=x[x$name>0,]
}
EDIT:
Example Code:
#First two lines work but those same two lines won't work if function is called
merged_data$name=as.numeric(merged_data$HETENURE)
merged_data=merged_data[merged_data$HETENURE>0,]
Remove_Missing(merged_data, HETENURE) #Call function
Data
structure(list(HRHHID = c("008906910993941", "008906910993941",
"648061954059610", "160916068405549", "160916068405549", "168069009100998"
), HRYEAR4 = c("2010", "2010", "2010", "2010", "2010", "2010"
), HETENURE = c(" 1", " 1", " 3", " 1", " 1", " 1"), HEFAMINC = c("11",
"11", "10", "13", "13", "14"), HRNUMHOU = c(" 2", " 2", " 1",
" 2", " 2", " 3"), GESTFIPS = c("01", "01", "01", "01", "01",
"01"), GTMETSTA = c("2", "2", "1", "1", "1", "1"), PEMARITL = c(" 1",
" 1", " 4", " 1", " 1", " 1"), PESEX = c(" 2", " 1", " 1", " 2",
" 1", " 2"), PEEDUCA = c("40", "45", "40", "42", "41", "39"),
PTDTRACE = c(" 1", " 1", " 1", " 1", " 1", " 1"), PEHSPNON = c(" 2",
" 2", " 2", " 2", " 2", " 2"), PEMLR = c(" 5", " 5", " 5",
" 1", " 1", " 7"), PRFTLF = c("-1", "-1", "-1", " 1", " 1",
"-1"), PRHRUSL = c("-1", "-1", "-1", " 4", " 4", "-1"), HESP1 = c("-1",
"-1", "-1", "-1", "-1", "-1"), HESP6 = c("-1", "-1", "-1",
"-1", "-1", "-1"), HESP7A = c("-1", "-1", "-1", "-1", "-1",
"-1"), HESP8 = c("-1", "-1", "-1", "-1", "-1", "-1"), HRFS12M1 = c(" 1", " 1", " 1", " 1", " 1", " 1")), row.names = c(9L, 10L, 11L,
12L, 13L, 15L), class = "data.frame")