split {base} R Documentation

## Divide into Groups

### Description

`split` divides the data in the vector `x` into the groups defined by `f`. The replacement forms replace values corresponding to such a division. `unsplit` reverses the effect of `split`.

### Usage

```split(x, f, drop = FALSE, ...)
split(x, f, drop = FALSE, ...) <- value
unsplit(value, f, drop = FALSE)
```

### Arguments

 `x` vector or data frame containing values to be divided into groups. `f` a “factor” in the sense that `as.factor(f)` defines the grouping, or a list of such factors in which case their interaction is used for the grouping. `drop` logical indicating if levels that do not occur should be dropped (if `f` is a `factor` or a list). `value` a list of vectors or data frames compatible with a splitting of `x`. Recycling applies if the lengths do not match. `...` further potential arguments passed to methods.

### Details

`split` and `split<-` are generic functions with default and `data.frame` methods.

`f` is recycled as necessary and if the length of `x` is not a multiple of the length of `f` a warning is printed. `unsplit` works only with lists of vectors. The data frame method can also be used to split a matrix into a list of matrices, and the replacement form likewise, provided they are invoked explicitly.

Any missing values in `f` are dropped together with the corresponding values of `x`.

### Value

The value returned from `split` is a list of vectors containing the values for the groups. The components of the list are named by the used factor levels given by `f`. (If `f` is longer than `x` then some of the components will be of zero length.)
The replacement forms return their right hand side. `unsplit` returns a vector for which `split(x, f)` equals `value`

### References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

`cut`

### Examples

```require(stats)
n <- 10; nn <- 100
g <- factor(round(n * runif(n * nn)))
x <- rnorm(n * nn) + sqrt(as.numeric(g))
xg <- split(x, g)
boxplot(xg, col = "lavender", notch = TRUE, varwidth = TRUE)
sapply(xg, length)
sapply(xg, mean)

## Calculate z-scores by group

z <- unsplit(lapply(split(x, g), scale), g)
tapply(z, g, mean)

# or

z <- x
split(z, g) <- lapply(split(x, g), scale)
tapply(z, g, sd)

## Split a matrix into a list by columns
ma <- cbind(x = 1:10, y = (-4:5)^2)
split(ma, col(ma))

split(1:10, 1:2)
```

[Package base version 2.5.0 Index]
{"ft_lang_label":"__label__en","ft_lang_prob":0.6892946,"math_prob":0.9875306,"size":2244,"snap":"2023-40-2023-50","text_gpt3_token_len":624,"char_repetition_ratio":0.11339286,"word_repetition_ratio":0.015,"special_character_ratio":0.27941176,"punctuation_ratio":0.16149068,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9951225,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-30T16:27:11Z\",\"WARC-Record-ID\":\"<urn:uuid:5b80c3c5-739e-44aa-aae3-459487590bac>\",\"Content-Length\":\"4096\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:24e195e9-2e78-4bf1-95e3-26b2ef0d338f>\",\"WARC-Concurrent-To\":\"<urn:uuid:efb299a2-f4c0-47c1-84bf-24de5e0fa0b8>\",\"WARC-IP-Address\":\"168.62.182.234\",\"WARC-Target-URI\":\"https://astrostatistics.psu.edu/su07/R/html/base/html/split.html\",\"WARC-Payload-Digest\":\"sha1:CJTC57L6XOME4BZ7EAJWM5ACJBAUJXDU\",\"WARC-Block-Digest\":\"sha1:O6VEPW4MASEZZYDPGLASVXCM6V3GLIPA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510697.51_warc_CC-MAIN-20230930145921-20230930175921-00871.warc.gz\"}"}