Extracting rows values based on a string


So i Need to extract the row values on the Low: , Average:, High:, Median: and i have been trying to use the grep function, but i can't get it to work. any suggestions?

You don't provide a lot of context information (or a reprex :wink:), so I have to guess here.

Your data is in a data.frame, your columns are named Col1, Col2, ... Your key is in Col2, the data you want is in Col4
To get Low: , you can use plain ol' R:

data$Col4[data$Col2 == 'Low:']

To get the full rows you can use

subset(data, Col2 %in% c("Low:", "Average:", "High:", "Median:"))

cheers
Steen

2 Likes

HAHA sorry about no reprex, but thanks! That got the job done for me.

For future reference would there be a way to do it if the same string appears multiple times in a csv file?

This kind of subset operation (or filter in dplyr) returns all the cases in which Col2 has a row with one of these values.

For example, if you have multiple rows in which Col2 had a value of "Low:", that subset will return all those rows. If you only wanted one specific row you'd need additional logic to extract the one row you want. (I hope you are getting a sense for why data scientists avoid spreadsheets like this!)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.