将单列拆分为四列并计算R中的重复模式 [英] Splitting single column into four columns and count repeated pattern in R

查看:49
本文介绍了将单列拆分为四列并计算R中的重复模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该项目的目的是了解在查看对象时如何获取信息.想象一个对象具有 a b c d e 之类的元素和 f .一个人可能会看着 a ,然后移至 b ,依此类推.现在,我们希望绘制并了解该人如何导航给定刺激的不同元素.我的数据在单个列中捕获了该移动,但是我需要将其分成几列以获取导航模式.请找到下面给出的示例.

Aim of this project is understand how information is acquired while looking into an object. Imagine an object has elements like a, b, c, d, e and f. A person might look at a and move onto to b and so forth. Now, we wish to plot and understand how that person have navigated across the different elements of a given stimuli. I have data that captured this movement in a single column but I need split this into few columns to get the navigation pattern. Please find the example given below.

我从数据帧中提取了列.现在,必须根据其特征将其分为四列.

I have column extracted from a data frame. Now it has to be split into four columns based on its characteristics.

a <- c( "a", "b", "b", "b", "a", "c", "a", "b", "d", "d", "d", "e", "f", "f", "e", "e", "f")
a <- as.data.frame(a)

预期产量

from   to   countfrom   countto

a      b      1           3
b      a      3           1
a      c      1           1
c      a      1           1
a      b      1           1
b      d      1           3
d      e      3           1      
e      f      1           2
f      e      2           2
e      f      2           1 

注意:我使用了 dplyr 从数据框中提取.

Note: I used dplyr to extract from the dataframe.

推荐答案

使用 rle 来获取每个字母的相对游程,然后将它们拼凑在一起:

Use rle to get the relative runs of each letter, and then piece it together:

r <- rle(a$a)
## or maybe `r <- rle(as.character(a$a)` depending on your R version
setNames(
    data.frame(lapply(r, head, -1), lapply(r, tail, -1)),
    c("countfrom","from","countto","to")
)
##   countfrom from countto to
##1          1    a       3  b
##2          3    b       1  a
##3          1    a       1  c
##4          1    c       1  a
##5          1    a       1  b
##6          1    b       3  d
##7          3    d       1  e
##8          1    e       2  f
##9          2    f       2  e
##10         2    e       1  f

这篇关于将单列拆分为四列并计算R中的重复模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆