繁体   English   中英

每隔一行添加到新列 - R

[英]Add Every Other Row to new Column - R

我正在寻找一种简单的方法将每隔一行添加到 R 的新列中。 我有 NCAA 篮球队在不同的、连续的排上互相比赛。 下面的示例:St. Joes 正在玩 La Salle,而 Connecticut 正在玩 Seton Hall,等等。我希望每个“游戏”都在同一条线上。 我已经搞砸了领先/滞后来解决这个问题,但这让我在第一行或最后一行出现错误,这取决于我使用哪一个。 这是我的数据示例:

# current data

Team       spread   price
St. Joes     -3     -105
La Salle      3     -115
Connecticut  -1.5   -105
Seton Hall    1.5   -115
Minnesota     5.5   -110
Penn State   -5.5   -110


# desired output below

Team1       spread1  price1  Team2        spread2  price2
St. Joes    -3       -105    La Salle     3        -115
Connecticut -1.5     -105    Seton Hall   1.5      -115
Minnesota    5.5     -110    Penn State  -5.5      -110

创建一组每两行并将行号分配为新列。 使用pivot_wider将数据转换为宽格式。

library(dplyr)
library(tidyr)

df %>%
  group_by(grp = ceiling(row_number()/2)) %>%
  mutate(row  =row_number()) %>%
  pivot_wider(names_from = row, values_from = Team:price) %>%
  ungroup %>%
  select(-grp)

#  Team_1      Team_2    spread_1 spread_2 price_1 price_2
#  <chr>       <chr>        <dbl>    <dbl>   <int>   <int>
#1 St.Joes     LaSalle       -3        3      -105    -115
#2 Connecticut SetonHall     -1.5      1.5    -105    -115
#3 Minnesota   PennState      5.5     -5.5    -110    -110

数据

df <- structure(list(Team = c("St.Joes", "LaSalle", "Connecticut", 
"SetonHall", "Minnesota", "PennState"), spread = c(-3, 3, -1.5, 
1.5, 5.5, -5.5), price = c(-105L, -115L, -105L, -115L, -110L, 
-110L)), class = "data.frame", row.names = c(NA, -6L))

使用来自dcastdata.table

library(data.table)
dcast(setDT(df)[, grp := gl(.N, 2, .N)], grp ~ rowid(grp),
      value.var = setdiff(names(df), 'grp'))[, grp := NULL][]
#        Team_1    Team_2 spread_1 spread_2 price_1 price_2
#1:     St.Joes   LaSalle     -3.0      3.0    -105    -115
#2: Connecticut SetonHall     -1.5      1.5    -105    -115
#3:   Minnesota PennState      5.5     -5.5    -110    -110

数据

df <- structure(list(Team = c("St.Joes", "LaSalle", "Connecticut", 
"SetonHall", "Minnesota", "PennState"), spread = c(-3, 3, -1.5, 
1.5, 5.5, -5.5), price = c(-105L, -115L, -105L, -115L, -110L, 
-110L)), class = "data.frame", row.names = c(NA, -6L))

使用reshape的基本 R 选项

reshape(
  cbind(df, p = rep_len(1:2, nrow(df)), q = ceiling(seq(nrow(df)) / 2)),
  direction = "wide",
  idvar = "q",
  timevar = "p"
)

  q      Team.1 spread.1 price.1    Team.2 spread.2 price.2
1 1     St.Joes     -3.0    -105   LaSalle      3.0    -115
3 2 Connecticut     -1.5    -105 SetonHall      1.5    -115
5 3   Minnesota      5.5    -110 PennState     -5.5    -110

暂无
暂无

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

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