Closed ctmccull closed 2 years ago
@ctmccull Have you defined vector? Here is an example how to use rv <- c("KHUSHI", "KRUNAL", "MATE", "AUS") grepl("K", rv)
So it looks like some progress is made when I include the data as an argument, but it still doesn't return anything. In the code below, I also specified returning the root.
varsearch <- function(string, data)
{
these <- grepl( "string", data, ignore.case=T )
dat.sub <- data[ these, ]
return( dat.sub$root )
}
varsearch("veteran", dd)
varsearch("veteran", dd) character(0)
If you are not yet comfortable writing functions you can break it into two steps: define arguments and the "recipe" component (function code).
The main thing to pay attention to is that every variable inside of the function must be either passed as an argument or defined somewhere, or the code won't run. Which variables here are created within the function, and which need to be passed to the function?
{
these <- grepl( string, vector, ignore.case=T )
dat.sub <- dd[ these, ]
return( dat.sub )
}
# NEEDS:
# dd
# string
# vector
Broken out into steps:
# ARGUMENTS
# dd is the data dictionary data frame
dat <- read.csv( "file-name.csv" )
vector <- dd$variable.name # or whatever the actual varname is called
string <- "blk"
# RECIPE:
# find all variable names that match the string
# return subset of data dictionary with matches
these <- grepl( string, vector, ignore.case=T )
dat.sub <- dat[ these, ]
dat.sub # print it
# PUT IT TOGETHER
# pass string to function as an argument (using a default here for demo)
# create vector of variable names internally
varsearch <- function( string, dat )
{
vector <- dat$variable.names
these <- grepl( string, vector, ignore.case=T )
dat.sub <- dat[ these, ]
return( dat.sub )
}
# TEST IT
varsearch( string="blk" )
@ctmccull You should be defining a variable named string then passing it to the string search function grep():
string <- "blk"
grepl( string, data, ignore.case=T )
You are currently searching for the word "string", not whatever you have assigned to the variable called string:
grepl( "string", data, ignore.case=T )
@lecy Thanks for your help. I also found that I wasn't properly defining vector. I didn't define string, because people need to be able to use the function to search for different strings, so it stands in as an argument of the function. When I defined vector as the variable description, it worked!
Thank you, @u12345 & @lecy.
@ctmccull, I'm glad you got it working.
While this issue has been resolved, I am leaving it open for now since it covers helpful strategies on writing functions.
@yukicruz @lecy
Hi again! So a team member was supposed to make the third function, but was unable to and asked me to do it. Anyway, I'm having a similar issue as the second function. I'm pretty sure it all comes down to defining vector. I'm just not sure how to get the function to return the variable names, but based on whether the time period variables are populated.
Here is what I tried:
filter_time <- function(time)
{
vector <- c(dd$`1970.f`, dd$`1970.s`, dd$`1980.f`, dd$`1980.s`, dd$`1990.f`, dd$`1990.s`, dd$`2000.f`, dd$`2000.s`, dd$`2010.f`, dd$`2010.s`)
these <- grep(time, vector, ignore.case = T)
dat.time <- data.frame(dd[these,])
return( dat.time )
}
Hi,
For example, dd<-read.csv("LTDB-DATA-DICTIONARY.CSV") print(dd$definition)
Rstudio gives the option to select your variable when you type $.
Ujitha
From: Caitlyn McCullers @.> Sent: Tuesday, March 29, 2022 5:59 PM To: Watts-College/cpp-528-spr-2022 @.> Cc: US @.>; Mention @.> Subject: Re: [Watts-College/cpp-528-spr-2022] Lab 2 - Trouble Creating the Second Function (Issue #4)
@yukicruzhttps://github.com/yukicruz @lecyhttps://github.com/lecy
Hi again! So a team member was supposed to make the third function, but was unable to and asked me to do it. Anyway, I'm having a similar issue as the second function. I'm pretty sure it all comes down to defining vector. I'm just not sure how to get the function to return the variable names, but based on whether the time period variables are populated.
Here is what I tried:
filter_time <- function(time)
{
vector <- c(dd$1970.f
, dd$1970.s
, dd$1980.f
, dd$1980.s
, dd$1990.f
, dd$1990.s
, dd$2000.f
, dd$2000.s
, dd$2010.f
, dd$2010.s
)
these <- grep(time, vector, ignore.case = T)
dat.time <- data.frame(dd[these,])
return( dat.time )
}
— Reply to this email directly, view it on GitHubhttps://github.com/Watts-College/cpp-528-spr-2022/issues/4#issuecomment-1082412624, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AD6Q7XXBCNA4JYYFKOH37T3VCN4NRANCNFSM5R4LXNGQ. You are receiving this because you were mentioned.Message ID: @.***>
@ctmccull, you currently only have one parameter (time) in your function. Consider the other arguments that need to be passed to the function.
Help please, am I on the right track for function 3?
vector <- c(dd$X1970.f, dd$X1970.s, dd$X1980.f, dd$X1980.s, dd$X1990.f,dd$X1990.s, dd$X2000.f, dd$X2000.s, dd$X2010.f, dd$X2010.s)
string <- "^hs"
timesearch <- function( string, dat ) { vector <- c(dd$X1970.f, dd$X1970.s, dd$X1980.f, dd$X1980.s, dd$X1990.f,dd$X1990.s, dd$X2000.f, dd$X2000.s, dd$X2010.f, dd$X2010.s) these <- grepl( string, vector, ignore.case=T ) dd.sub <- dd[ these, ] return( dd.sub ) }
In addition to the steps that Professor Lecy provided yesterday, also check for common coding errors.
Find three common error types here and additional types here.
@mrowland1, consider syntax error types. For example, is X1970.f
supposed to be a variable or string?
@yukicruz I see that error, thanks. I'm confused on how to check variable availability for each year.
Hello! I'm working on making the search function for Lab 2 and keep running across an error. Would anyone be able to help? @yukicruz
Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character'
I have also tried changing where it says 'vector' to dd to see if that was the issue, but it doesn't seem to make a difference.