Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

JASP compute new column with R and test for substring

In a survey I have a column "Equipment" containing rows with video equipment names separated with commas:

"Camera, Light, Computer, Software, Hard Drives"

"Camera, Computer, Software, Hard Drives"

"Light, Microphone, Computer, Software"

I would like to make a new column "Camera" containing TRUE if the substring "Camera" is

present it the int the "Equipment" Column.


I tried

grep("Camera", Equipment, fixed=TRUE)

This just returns the lines with the matching records and is not applied to each row.

Is there a test function in the graphical editor for testing if string contains substring?

Please help.

Comments

  • edited February 2021

    I'm afraid that that isn't supported right now, but I'll be adding support for it so it will be possible in the next version.


    You can do it in the "R" computed column btw with something like:

    Equipment %in% grep("Camera", Equipment, fixed=TRUE, value=TRUE)

  • So the function in R that you're looking for is grepl(pattern, x) , which returns TRUE if an element of x contains the pattern (e.g., Camera). Unfortunately, this function is not in our whitelist so that won't work for now. In the meantime, you can use the following workaround:

    res <- rep(0, 100)
    res[grep("Camera", Equipment)] <- 1
    res
    

    here Equipment should contain the names that you want to match on. You could add fixed = true, but it should not matter for the result.


    Hope that helps!

  • jgoosen and vandenman you are amazing! thanks you so much!!!

Sign In or Register to comment.