本文介绍了R中数据框中的重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码在数据框中复制行。
但是,我发现它很慢。
I am trying to duplicate rows in my data frame using the code below.However, I'm finding it to be slow.
duprow = df[1,]
for(i in 1:2000)
{
print(i)
df = rbind(df,duprow)
}
有更快的方法吗?
推荐答案
这是我的破解它:
> # create an example data frame
> colornames=c("violet","indigo","blue","green","yellow","orange","red")
> wavelength=c(400,425,470,550,600,630,665)
> df <- data.frame(colornames, wavelength)
>
> # How many replicates you want of each row
> duptimes <- c(0,1,2,1,1,4,1)
>
> # Create an index of the rows you want with duplications
> idx <- rep(1:nrow(df), duptimes)
>
> # Use that index to genderate your new data frame
> dupdf <- df[idx,]
>
> # display results
> df
colornames wavelength
1 violet 400
2 indigo 425
3 blue 470
4 green 550
5 yellow 600
6 orange 630
7 red 665
> dupdf
colornames wavelength
2 indigo 425
3 blue 470
3.1 blue 470
4 green 550
5 yellow 600
6 orange 630
6.1 orange 630
6.2 orange 630
6.3 orange 630
7 red 665
我不知道这样做是否更快,但是它不需要加载其他软件包,也可以删除不需要的行。
I don't know if this is any faster, but it doesn't require loading additional packages and also removes unwanted rows.
缺点是您需要对数据框中的每一行做出决策,但这并不难于编写代码。
Downside is you need to make decisions about each row in the data frame, but that shouldn't be too difficult to code in.
这篇关于R中数据框中的重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!