English 中文(简体)
我怎么能够把扼杀性标签和数字价值分为R?
原标题:How can I split a string into character labels and numeric values in R?

在这里,请作宽恕的解释,让我知道我需要澄清的内容。

我有这样的目标,即:

"aaaa,1,1,1,1,0"
"abba,0,0,1,1,1"
"bbaa,1,0,0,0,1"

我愿把四字母的标签分开,作为矢量,我还要重新编成编号,以便我最终掌握像这样的数据框架、3个包含6个变量的斜体,标明为特性和数字:

aaaa 1 1 1 1 0
abba 0 0 1 1 1
bbaa 1 0 0 0 1

然后,我想在上面加上“col标签”。

NAME 1 2 3 4 5
aaaa 1 1 1 1 0
abba 0 0 1 1 1
bbaa 1 0 0 0 1

并且我也像一个名词。

我感到有点和点。 我可以使用四封信贴上标签。

substr(数据,1,4), 并努力使病媒成为病媒

“aaaa”、“abba”、“bbaa”。

但是,我无法说明用什么来获取其他扼杀手段,即部分,作为病媒。 substr (data,5,last) don t work and 我请我不要说substr(data,5,14),因为尽管我在此举出了例子,但座标语始终为14个特性。 是否有办法具体指明<代码>(数据,5,”到插图末?”

接着,为了将扼杀变成我试图试图把的人数。

as.integer (unlist (strsplit(data,”))

在原始档案中,我用1和0的单长病媒回来,但在标签上,“aaba”被“NAs”取代。

我不想把所有东西混在一起。

[如果我有数字和标签,那么,我首先会问: 因为我想用“1,1,1”取代所有“1,01”,并使用<代码>paste(,将数字改为指示数,并使用<编码>gsub(<>>,使替换成为我唯一能够开展工作的方法。]

最佳回答

你开始走上正确的道路。 我必须再补充几个步骤,但我还要努力:

input = c(
    "aaaa,1,1,1,1,0",
    "abba,0,0,1,1,1",
    "bbaa,1,0,0,0,1"
)

df = type.convert(
    as.data.frame(
        matrix(unlist(strsplit(input,  , )), byrow = TRUE, nrow = length(input))),
    as.is = TRUE
)
问题回答

两种选择:

  1. 在R基数中,substr(数据,5年,(数据))

  2. 如果您愿意使用另一个包件,请上stringr。 然后,你只能使用-1来表明病媒的终结。 Try stringr:str_sub(data, 5, -1)

I think BigFinger s answer performs almost all of the desired functionality but in order to change the column names as well, you can add names(df) <- c("NAME",1,2,3,4,5).





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签