Find string in data.frame

后端 未结 4 1701
清歌不尽
清歌不尽 2021-01-05 01:12

How do I search for a string in a data.frame? As a minimal example, how do I find the locations (columns and rows) of \'horse\' in this data.frame?



        
4条回答
  •  情歌与酒
    2021-01-05 01:53

    Another way to do it is the following:

    library(data.table)
    library(zoo)
    library(dplyr)
    library(timeDate)
    library(reshape2)
    data frame name = tbl_account
    

    first,Transpose it :

    temp = t(tbl_Account)
    

    Then, put it in to a list :

    temp = list(temp)
    

    This essentially puts every single observation in a data frame in to one massive string, allowing you to search the whole data frame in one go.

    then do the searching :

    temp[[1]][grep("Horse",temp[[1]])] #brings back the actual value occurrences
    grep("Horse", temp[[1]]) # brings back the position of the element in a list it occurs in 
    

    hope this helps :)

提交回复
热议问题