如何在R中分离没有空格的因子?

时间:2017-02-14 17:18:56

标签: r split

我想将一个因子分成14行,每行就像"cg17205324 (Adolescence)"

我尝试了strsplit(),但始终以"cg17205324 "结束。

使用Google搜索各种方法来清除尾部whitespace但不起作用,因为它是factor而不是string

任何提示?

3 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

 "cg17205324 (Adolescence)" -> outp
 strsplit(outp," ") # " " serves as space and separate the two strings 
[[1]]
[1] "cg17205324"    "(Adolescence)"

答案 1 :(得分:0)

我们可以使用<link data-require="bootstrap@4.0.0-alpha.6" data-semver="4.0.0-alpha.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" /> <div class="card-deck"> <div class="card"> <div class="card-block"> <h4 class="card-title">Card title</h4> <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small> </p> </div> </div> <div class="card"> <div class="card-block"> <h4 class="card-title">Card title</h4> <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small> </p> </div> </div> <div class="card"> <div class="card-block"> <h4 class="card-title">Card title</h4> <p class="card-text">supportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingto additional content. This card has even longer content than the first to show that equal height action.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small> </p> </div> </div> </div>

scan

数据

scan(text=str1, what ="", quiet=TRUE)
#[1] "cg17205324"    "(Adolescence)"

答案 2 :(得分:0)

a <- "cg17205324 (Adolescence)"

b <- strsplit(a, " ")
b

#[[1]]
#[1] "cg17205324"    "(Adolescence)"