R中的数据帧修改

R中的数据帧修改

本文介绍了R中的数据帧修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的R代码的三行显示向量a1,b1和 c1,这是字段a1的唯一性。我想显示两列的数据框,在其中我应该让c1的每个元素显示的次数等于一列中 a1中元素的长度,而另一列中该字母的对应ID b1柱。简单地说,说一个带有 y列的数据帧,其中 c1中的字母 a将被背对背表示6次(字符串a1的长度),然后 b表示6次,然后是c,依此类推。在另一列中也对应于 1 6次,然后 2 6次,依此类推。请帮忙,谢谢。

the three lines of R code below show a vector a1,b1 and "c1" which is the unique of fields a1. I want to display a data frame of two columns where I should get every element of c1 displayed the number of times equal to the length of elements in "a1", in one column, and the corresponding ID "b1" of that letter in another column. Simply, say a data frame with column "y" in which say letter "a" from "c1" will be represented 6 times back to back(length of string a1), then "b" 6 times, then c and so on. Also corresponding to a in other column, "1" 6 times, then "2" 6 times and so on. Please help and thanks.

a1 = c("a","b","c","d","a","b")
b1 = c(1,2,3,4,1,2)
c1 = unique(a1)

新更改

a1 = c("a","b","b","d","c","e","f","a","b","c","d")
b1 = c(1,1,1,2,3,2,3,1,1,3,2)
c1 = unique(a1)


推荐答案

这可以实现您所相信的:

This achieves what you ask I believe:

cbind(rep(c1, each = length(a1)),rep(b1, each = length(a1)))

这篇关于R中的数据帧修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:15