Watts-College / cpp-528-spr-2022

https://watts-college.github.io/cpp-528-spr-2022/
0 stars 0 forks source link

grepl/stringr function build issues #6

Closed krbrick closed 2 years ago

krbrick commented 2 years ago

I was able to write a function for time by first wrangling time period variables, but I am struggling to write a function for finding strings in data.

here is my (nonworking) function:

`find.variables.for.string <- function(variable.string) { test.term <- str_detect(dd$definition,"variable.string") test.term.l <- cbind (dd,test.term) %>% filter(test.term == T) return(test.term.l) }

find.variables.for.string(asian)`

my return gives me

find.variables.for.string(asian) [1] root root2 category definition X1970.f X1970.s X1980.f X1980.s
[9] X1990.f X1990.s X2000.f X2000.s X2010.f X2010.s X X.1
[17] X.2 X.3 X.4 X.5 X.6 X.7 test.term

<0 rows> (or 0-length row.names)

same with: find.string <- function(string) { variable.string <- dd %>% filter(str_detect(definition, "string")) return(variable.string) }

find.string(poverty)

the inside bits of the functions work on their own, giving a datatable:

`asian.test <- str_detect(dd$definition,"Asian") asian.test.2 <-cbind(dd,asian.test) %>% filter(asian.test == T) View(asian.test.2)

and

dd %>% filter(str_detect(definition, "string"))`

any thoughts on where I'm going wrong? thanks in advance

u12345 commented 2 years ago

@krbrick I ran your non-working code, I am not getting the same error message, it returns (43 + 237) rows. Do you have library(stringr) loaded?

krbrick commented 2 years ago

Hi! yeah it is loaded did it work for you?

u12345 commented 2 years ago

@krbrick I copied your exact code, it worked for me.

krbrick commented 2 years ago

thank you, something is wrong with my here() setting and i think that is giving me issues.