拆分向量由n个零分成不同的组

时间:2017-08-30 19:12:27

标签: r

我有一个向量x

x = c(1, 1, 2.00005, 1, 1, 0, 0, 0, 0, 1, 2, 0, 3, 4, 0, 0, 0, 0, 1, 2, 3, 1, 3)

我需要将n分隔的值(在这种情况下,假设n3)或更多零分成不同的组。

所需的输出将是

list(x1 = c(1, 1, 2.00005, 1, 1),
     x2 = c(1, 2, 0, 3, 4),
     x3 = c(1, 2, 3, 1, 3))
#$x1
#[1] 1.00000 1.00000 2.00005 1.00000 1.00000

#$x2
#[1] 1 2 0 3 4

#$x3
#[1] 1 2 3 1 3

以下内容不起作用,因为即使组中的x个小于零,它也会分割n

temp = cumsum(x == 0)
split(x[x!=0], temp[x!=0])
#$`0`
#[1] 1.00000 1.00000 2.00005 1.00000 1.00000

#$`4`
#[1] 1 2

#$`5`
#[1] 3 4

#$`9`
#[1] 1 2 3 1 3

5 个答案:

答案 0 :(得分:6)

Here's my attempt at it. This method replaces runs of zero that are length less than or equal to 3 with NA. Since NA is removed when using hover, we are left with the desired output.

.dropDownContents{
    display:none;
}
.dropDownbtn:hover .dropDownContents{
    color:white;
    background-color:black;
    border:solid 1px white;
    display: block;
}

答案 1 :(得分:5)

以下是rlesplitlapply

的方法
# get RLE
temp <- rle(x)
# replace values with grouping variables
temp$values <- cumsum(temp$values == 0 & temp$lengths > 2)

# split on group and lapply through, dropping 0s at beginning which are start of each group
lapply(split(x, inverse.rle(temp)), function(y) y[cummax(y) > 0])
$`0`
[1] 1.00000 1.00000 2.00005 1.00000 1.00000

$`1`
[1] 1 2 0 3 4

$`2`
[1] 1 2 3 1 3

没有lapply的第二种方法如下

# get RLE
temp <- rle(x)
# get positions of 0s that force grouping
changes <- which(temp$values == 0 & temp$lengths > 2)
# get group indicators
temp$values <- cumsum(temp$values == 0 & temp$lengths > 2)
# make 0s a new group
temp$values[changes] <- max(temp$values) + 1L

# create list
split(x, inverse.rle(temp))
$`0`
[1] 1.00000 1.00000 2.00005 1.00000 1.00000

$`1`
[1] 1 2 0 3 4

$`2`
[1] 1 2 3 1 3

$`3`
[1] 0 0 0 0 0 0 0 0

最后,您只需删除最后一个列表项,例如head(split(x, inverse.rle(temp)), -1)

答案 2 :(得分:3)

This method is just slightly different from what you already proposed, and includes a first step of replacing all stretches of n or more zeroes by a value not found in x, for example max+1:

Welcome to Canopy's interactive data-analysis environment!'t' not in 'tf'
Type '?' for more information.

...:
...:
n [1]: %run "C:\Projects\MyProject\MyProgram.py"

Enter (T)est or (F)ull or (Q)uit: t

How many rows to process: d

't' not in 'tf'
Out[2]: False

'a' not in 'tf'
Out[3]: True
In [4]: 
...:
...:
...:

答案 3 :(得分:3)

Yet another solution using private string strTitle, strText, strImageurl; public Add_New_President_Class(string nTitle, string nText, string nImageurl) { strTitle = nTitle; strText = nText; strImageurl = nImageurl; }//constructor public void createNewPresident() { String retTitle = DataAccess.createNewPresident(strTitle, strText, strImageurl); strTitle = retTitle; } public string getTitle() { return strTitle; } (twice) and protected void btnAddNewPresident_Click(object sender, EventArgs e) { String imageName = fuImagePresident.FileName; fuImagePresident.PostedFile.SaveAs(Server.MapPath("../Home_Images/president.jpg")); String url = "../Home_Images/president.jpg"; String textReplaced; textReplaced = txtPresidentText.Text.Replace("\n", "<br />"); Add_New_President_Class newPresident = new Add_New_President_Class(txtPresidentTitle.Text, textReplaced, url); newPresident.createNewPresident(); Session["newpresident"] = newPresident; txtPresidentTitle.Text = ""; txtPresidentText.Text = ""; Response.Write("<script>window.alert('A news president has been added')</script>"); } .

> $.each(this.points, function () {
>   s += '<br/><span style="color:' + this.series.color + '">\u25CF</span>' + this.series.name + ': ' + '<b>' + this.y + '</b>';
> });

In the mean time I realized that the code that prepares the loop above and the loop itself could be greatly simplified. In order to make it complete, I will repeat the initial lines of code.

rle

答案 4 :(得分:2)

以下是使用rleinverse.rle多次创建x(x_sub)和组号(group_sub)的子集的想法。最后,使用split获取最终结果。

x <- c(1, 1, 2.00005, 1, 1, 0, 0, 0, 0, 1, 2, 0, 3, 4, 0, 0, 0, 0, 1, 2, 3, 1, 3)

### Step 1: Filtet the index with values == 0 and length > 3
x2 <- as.integer(x != 0)
run <- rle(x2)
index <- which(run$values == 0 & run$lengths > 3)

### Step 2: Replace the values in index to -1
### Create an intermediate index (x3)
run2 <- run
run2$values[index] <- -1
run2$values[run2$values == 0] <- 1
x3 <- inverse.rle(run2)

### Step 3: Create grouping variable (x4)
run3 <- rle(x3)
run3$values <- 1:length(run3$values)
x4 <- inverse.rle(run3)

### Step 4: Subset x by x3 and x4 (x_sub) and create group number (group_sub)
x_sub <- x[x3 != -1]
group_sub <- x4[x3 != -1] %/% 2 + 1

### Step 5: Split x_sub to get the final output (final_list)
final_list <- split(x_sub, f = group_sub)

final_list
$`1`
[1] 1.00000 1.00000 2.00005 1.00000 1.00000

$`2`
[1] 1 2 0 3 4

$`3`
[1] 1 2 3 1 3