简体   繁体   English

合并 R 中的每隔一行

[英]merge every other row in R

I have this dataframe where the id and values are on two alternating rows.我有这个数据框,其中 id 和值位于两个交替行上。

278             
8134134 87903   98732   1248    
9907        
3134 1450893    1345807 1234    555    
23              
23487   12347   3407        

I am trying to append the values to the ids so they are on the same row.我正在尝试将值附加到 ids 以便它们在同一行上。

278 8134134 87903   98732   1248    
9907    3134    1450893 1345807 1234    555    
23  23487   12347   3407    

(Also wondering how to format tables in these questions!) (还想知道如何在这些问题中格式化表格!)

It's unclear what your data structure really looks like.目前尚不清楚您的数据结构究竟是什么样子。 I've taken my best guess.我已经尽力了。 In principle, you can get alternate rows using a c(TRUE, FALSE) index and relying on recycling.原则上,您可以使用c(TRUE, FALSE)索引并依靠回收来获取备用行。

df <- 
  data.frame(x = c("278",
                   "8134134 87903 98732 1248",
                   "9907",
                   "3134 1450893 1345807 1234 555",
                   "23",
                   "23487 12347 3407"),
             stringsAsFactors = FALSE)

cbind(df[c(TRUE, FALSE), ],
      df[c(FALSE, TRUE), ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM