grepl

Use grep to find letter without separate

独自空忆成欢 提交于 2020-01-17 06:09:29
问题 I have these codes: x=c('a','a,b','a-c','ab') y=c('a') grep(y,x,ignore.case = T) The result is > grep(y,x) [1] 1 2 3 4 But I expect that the result should be "1 2 3", once "a" is separated by anything or just "a", except "a" is not separated like "ab". Thank you! 回答1: Add a word boundary to y : x=c('a','a,b','a-c','ab') y=c('a\\b') grep(y,x,ignore.case = T) # [1] 1 2 3 回答2: As the OP's wants to have a pattern that involves not having any letters following 'a' ( [^a-z] ) or ( | ) it can be the

Apply a function if column name is in a list in R?

青春壹個敷衍的年華 提交于 2020-01-07 08:02:07
问题 Okay, I am probably pursuing a roundabout way of accomplishing this, but I had a list called " goodAttributes " and a dataframe called QTable . If a column name of QTable is in " goodAttributes " than I want to return the max of that column, otherwise I want to return the minimum of the column... I checked if the column name of QTable was in the list " goodAttributes " using " grepl " and then reset the column names of QTable if the value was in the list " goodAttributes " as "True" and

In R, find the column that contains a string in for each row

喜你入骨 提交于 2020-01-04 02:31:14
问题 I must be thinking in the wrong search terms because I cannot believe my question is unique, but I only found one similar. I have some rather clunky data from the World Bank that is a flat file representing a database. The data are one project per row, but each project has multiple characteristics that are conveniently in columns with names like, "SECTOR.1" with it's own characteristics in other columns with names like, "SECTOR.1.PCT" etc. From this, I'm trying to extract the data that are

In R, find the column that contains a string in for each row

人盡茶涼 提交于 2020-01-04 02:31:12
问题 I must be thinking in the wrong search terms because I cannot believe my question is unique, but I only found one similar. I have some rather clunky data from the World Bank that is a flat file representing a database. The data are one project per row, but each project has multiple characteristics that are conveniently in columns with names like, "SECTOR.1" with it's own characteristics in other columns with names like, "SECTOR.1.PCT" etc. From this, I'm trying to extract the data that are

Concatenate previous and latter words to a word that match a condition in R

走远了吗. 提交于 2020-01-04 00:19:12
问题 I need to concatenate the previous and the latter words of a condition meeting word. Specifically, those who match the condition of having a comma. vector <- c("Paulsen", "Kehr,", "Diego", "Schalper", "Sepúlveda,", "Diego") #I know how to get which elements meet my condition: grepl(",", vector) #[1] FALSE TRUE FALSE FALSE TRUE FALSE Desired output: print(vector_ok) #[1] "Paulsen Kehr, Diego", "Schalper Sepúlveda, Diego" Thanks in advance! 回答1: You can use grep() to get the positions of the

Making a character string with column names with zero values

隐身守侯 提交于 2019-12-31 04:28:05
问题 The 4th column is my desired column. Video,Webinar,Meeting,Conference are the 4 type of activities that the different customers(names) can engage in. You can see,in a given row, all the column names with zero value are in the final column(NextStep) and the value there(character string separated by commas) excludes the column name with non-zero value. The character strings(column names) in the final column usually appear in the column order with two exceptions. Webinar always appears first if

Exact match with grepl R

北城余情 提交于 2019-12-31 02:42:28
问题 I'm trying to extract certain records from a dataframe with grepl. This is based on the comparison between two columns Result and Names. This variable is build like this "WordNumber" but for the same word I have multiple numbers (more than 30), so when I use the grepl expression to get for instance Word1 I get also results that I would like to avoid, like Word12. Any ideas on how to fix this? Names <- c("Word1") colnames(Names) <- name Results <- c("Word1", "Word11", "Word12", "Word15")

R - return boolean if any strings in a vector appear in any of several columns

别来无恙 提交于 2019-12-25 07:54:09
问题 I have a large data frame, each row of which refers to an admission to hospital. Each admission is accompanied by up to 20 diagnosis codes in columns 5 to 24. Col1 Col2 Col3 Col4 Diag_1 Diag_2 Diag_3 ... Diag_20 data data data data J123 F456 H789 E468 data data data data T452 NA NA NA Separately, I have a vector ( risk_codes ) of length 136, all strings. These strings are risk codes that can be similar to the truncated diagnosis codes (e.g. J12 would be ok, F4 would be ok, H798 would not). I

grepl for dplyr sql table?

冷暖自知 提交于 2019-12-18 11:54:11
问题 is there a workaround to use something like filter(df, grepl("A|B|C",location)) for a dplyr SQL table? In SQL it is probalby a LIKE . Of cource I could convert the SQL table to a R data table, but it is very large. (http://cran.r-project.org/web/packages/dplyr/vignettes/databases.html) At the moment I get Error in sqliteSendQuery(conn, statement) : error in statement: no such function: GREPL thx Christof 回答1: Using sql to translate the expression directly into sql is one option. sql_table %>%

r- grepl to find multiple strings exists

*爱你&永不变心* 提交于 2019-12-18 04:25:17
问题 grepl("instance|percentage", labelTest$Text) will return true if any one of instance or percentage is present. How will i get true only when both the terms are present. 回答1: Text <- c("instance", "percentage", "n", "instance percentage", "percentage instance") grepl("instance|percentage", Text) # TRUE TRUE FALSE TRUE TRUE grepl("instance.*percentage|percentage.*instance", Text) # FALSE FALSE FALSE TRUE TRUE The latter one works by looking for: ('instance')(any character sequence)('percentage'