strsplit

R extract first number from string

本秂侑毒 提交于 2019-12-20 18:37:28
问题 I have a string in a variable which we call v1. This string states picture numbers and takes the form of "Pic 27 + 28". I want to extract the first number and store it in a new variable called item. Some code that I've tried is: item <- unique(na.omit(as.numeric(unlist(strsplit(unlist(v1),"[^0-9]+"))))) This worked fine, until I came upon a list that went: [1,] "Pic 26 + 25" [2,] "Pic 27 + 28" [3,] "Pic 28 + 27" [4,] "Pic 29 + 30" [5,] "Pic 30 + 29" [6,] "Pic 31 + 32" At this point I get more

R extract first number from string

匆匆过客 提交于 2019-12-20 18:37:13
问题 I have a string in a variable which we call v1. This string states picture numbers and takes the form of "Pic 27 + 28". I want to extract the first number and store it in a new variable called item. Some code that I've tried is: item <- unique(na.omit(as.numeric(unlist(strsplit(unlist(v1),"[^0-9]+"))))) This worked fine, until I came upon a list that went: [1,] "Pic 26 + 25" [2,] "Pic 27 + 28" [3,] "Pic 28 + 27" [4,] "Pic 29 + 30" [5,] "Pic 30 + 29" [6,] "Pic 31 + 32" At this point I get more

How to vectorize R strsplit?

本秂侑毒 提交于 2019-12-20 09:39:19
问题 When creating functions that use strsplit , vector inputs do not behave as desired, and sapply needs to be used. This is due to the list output that strsplit produces. Is there a way to vectorize the process - that is, the function produces the correct element in the list for each of the elements of the input? For example, to count the lengths of words in a character vector: words <- c("a","quick","brown","fox") > length(strsplit(words,"")) [1] 4 # The number of words (length of the list) >

strsplit and lapply

若如初见. 提交于 2019-12-20 04:13:01
问题 I have a string in some text of the form "12,34,77" , including the quotation marks. I need to get the values of each of those numbers into a list. I tried using lapply and strsplit : control2=lapply(strsplit(data$values,","),as.numeric) but I get the error: non character argument What am I doing wrong? 回答1: 1) strapply 1a) scalar Here is a one-liner using strapply from the gsubfn package: library(gsubfn) x <- '"12,34,567"' strapply(x, "\\d+", as.numeric, simplify = c) ## [1] 12 34 567 1b)

strsplit by spaces greater than one in R

天涯浪子 提交于 2019-12-20 02:58:17
问题 Given a string, mystr = "Average student score 88" I wish to split if there are more than 1 space. I wish to obtain the following: "Average student score" "88" I searched that "\s+" will split by any number of spaces. strsplit(mystr, "\\s+") But this is not what I want. Is there any option within strsplit that can split strings based on a certain number of spaces (say space = k) or a rule on spaces (say space > 1)? 回答1: You may specify it through a repetition quantifier. strsplit(mystr, "\\s

How to extract numbers from text? [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-19 04:55:40
问题 This question already has answers here : Extracting unique numbers from string in R (7 answers) Closed 3 years ago . i have the flowing text string: string <- "['CBOE SHORT-TERM VIX FUTURE DEC 2016', 81.64],\n\n ['CBOE SHORT-TERM VIX FUTURE JAN 2017', 18.36]" is there a simple way of extracting numerical elements from text without having to use: string_table <- strsplit(string, " ") and then select n-th element and continue to strsplit until i have what i need. the result should be: result <-

apply strsplit rowwise

限于喜欢 提交于 2019-12-18 10:29:47
问题 Im trying to split a string on "." and create additional columns with the two strings before and after ".". tes<-c("1.abc","2.di","3.lik") dat<-c(5,3,2) h<-data.frame(tes,dat) h$num<-substr(h$tes,1,1) h$prim<-unlist(strsplit(as.character(h$tes),"\\."))[2] h$prim<-sapply(h$tes,unlist(strsplit(as.character(h$tes),"\\."))[2]) I´d like h$prim to contain "abc","di","lik"..However I´m not able to figure it out. I guess strsplit is not vectorized, but then I thought the sapply version should have

Split a string by any number of spaces

混江龙づ霸主 提交于 2019-12-17 10:44:37
问题 I have the following string: [1] "10012 ---- ---- ---- ---- CAB UNCH CAB" I want to split this string by the gaps, but the gaps have a variable number of spaces. Is there a way to use strsplit() function to split this string and return a vector of 8 elements that has removed all of the gaps? One line of code is preferred. 回答1: Just use strsplit with \\s+ to split on: x <- "10012 ---- ---- ---- ---- CAB UNCH CAB" x # [1] "10012 ---- ---- ---- ---- CAB UNCH CAB" strsplit(x, "\\s+")[[1]] # [1]

Chopping a string into a vector of fixed width character elements

帅比萌擦擦* 提交于 2019-12-17 01:07:31
问题 I have an object containing a text string: x <- "xxyyxyxy" and I want to split that into a vector with each element containing two letters: [1] "xx" "yy" "xy" "xy" It seems like the strsplit should be my ticket, but since I have no regular expression foo, I can't figure out how to make this function chop the string up into chunks the way I want it. How should I do this? 回答1: Using substring is the best approach: substring(x, seq(1, nchar(x), 2), seq(2, nchar(x), 2)) But here's a solution with

using strsplit and subset in dplyr and mutate

China☆狼群 提交于 2019-12-14 03:40:23
问题 I have a data table with one string column. I'd like to create another column that is a subset of this column using strsplit. dat <- data.table(labels=c('a_1','b_2','c_3','d_4')) The output I want is label sub_label a_1 a b_2 b c_3 c d_4 d I've tried the followings but neither seems to work. dat %>% mutate( sub_labels=strsplit(as.character(labels), "_")[[1]][1] ) # gives a column whose values are all "a" this one, which seems logical to me, dat %>% mutate( sub_labels=sapply(strsplit(as