每个n列都宽至长时间

发布于 2025-01-22 03:16:35 字数 607 浏览 2 评论 0原文

假设我有一个dataframe:

dw <- read.table(header=T, text='
 ID     q1    q2   q3     q4     q5    ...q10  
   A   10    6     50     10      bA   
   B   12    5     70     11      bB
   C   20    7     20     8       bC
   D   22    8     22     9       bD
 ')

我想在'id'之后移动每2列到新行,因此看起来像:

   ID  q1   q2 
   A   10    6     
   B   12    5     
   C   20    7     
   D   22    8 
   A   50    10
   B   70   11
   C   20   8
   D   22   9
   ....

pivot_longer似乎可以移动每个列而不是多个列列?

Suppose I have a dataframe:

dw <- read.table(header=T, text='
 ID     q1    q2   q3     q4     q5    ...q10  
   A   10    6     50     10      bA   
   B   12    5     70     11      bB
   C   20    7     20     8       bC
   D   22    8     22     9       bD
 ')

I would like to move every 2 columns after 'ID' to new rows so it looks like:

   ID  q1   q2 
   A   10    6     
   B   12    5     
   C   20    7     
   D   22    8 
   A   50    10
   B   70   11
   C   20   8
   D   22   9
   ....

pivot_longer seems to move every single column instead of multiple columns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆依然 2025-01-29 03:16:35

似乎您不关心列名(id),而它们都是同一类。为此,我们可以手动“旋转”,而无需pivot_lower的保障或功能,但也没有要求。

第一步是确保class不会成为问题。因为您在那里有一些字符串,所以我们需要将全部转换为targue

dw[-1] <- lapply(dw[-1], as.character)

之后,我们可以手动提取每两个(non- id)列,并与id :

cols <- seq_along(dw)[-1]
list_of_frames <- lapply(split(cols, cols %/% 2), function(ind) setNames(dw[,c(1, ind)], c("ID", "q1", "q2")))
list_of_frames
# 

似乎您不关心列名( id ),而它们都是同一类。为此,我们可以手动“旋转”,而无需 pivot_lower 的保障或功能,但也没有要求。

第一步是确保 class 不会成为问题。因为您在那里有一些字符串,所以我们需要将全部转换为 targue

dw[-1] <- lapply(dw[-1], as.character)

之后,我们可以手动提取每两个(non- id )列,并与 id :

1` # ID q1 q2 # 1 A 10 6 # 2 B 12 5 # 3 C 20 7 # 4 D 22 8 #

似乎您不关心列名( id ),而它们都是同一类。为此,我们可以手动“旋转”,而无需 pivot_lower 的保障或功能,但也没有要求。

第一步是确保 class 不会成为问题。因为您在那里有一些字符串,所以我们需要将全部转换为 targue

dw[-1] <- lapply(dw[-1], as.character)

之后,我们可以手动提取每两个(non- id )列,并与 id :

2` # ID q1 q2 # 1 A 50 10 # 2 B 70 11 # 3 C 20 8 # 4 D 22 9 #

似乎您不关心列名( id ),而它们都是同一类。为此,我们可以手动“旋转”,而无需 pivot_lower 的保障或功能,但也没有要求。

第一步是确保 class 不会成为问题。因为您在那里有一些字符串,所以我们需要将全部转换为 targue

dw[-1] <- lapply(dw[-1], as.character)

之后,我们可以手动提取每两个(non- id )列,并与 id :

3` # ID q1 q2 # 1 A bA zA # 2 B bB zB # 3 C bC zC # 4 D bD zD

这可以轻松地与几种方法结合在一起,选择一种:

data.table::rbindlist(list_of_frames)
dplyr::bind_rows(list_of_frames)
do.call(rbind, list_of_frames)
#    ID q1 q2
# 1   A 10  6
# 2   B 12  5
# 3   C 20  7
# 4   D 22  8
# 5   A 50 10
# 6   B 70 11
# 7   C 20  8
# 8   D 22  9
# 9   A bA zA
# 10  B bB zB
# 11  C bC zC
# 12  D bD zD

数据之一

dw <- structure(list(ID = c("A", "B", "C", "D"), q1 = c("10", "12", "20", "22"), q2 = c("6", "5", "7", "8"), q3 = c("50", "70", "20", "22"), q4 = c("10", "11", "8", "9"), q5 = c("bA", "bB", "bC", "bD"), q6 = c("zA", "zB", "zC", "zD")), row.names = c(NA, -4L), class = "data.frame")

It seems that you are not concerned with the column names (other than ID), and that they are all the same class. For this, we can "pivot" manually, without the safeguards or power of pivot_lower perhaps, but without the requirements as well.

The first step is to make sure that class won't be an issue; because you have some strings in there, we need to convert all to character:

dw[-1] <- lapply(dw[-1], as.character)

After that, we can manually extract every two (non-ID) columns and combine with ID:

cols <- seq_along(dw)[-1]
list_of_frames <- lapply(split(cols, cols %/% 2), function(ind) setNames(dw[,c(1, ind)], c("ID", "q1", "q2")))
list_of_frames
# 

It seems that you are not concerned with the column names (other than ID), and that they are all the same class. For this, we can "pivot" manually, without the safeguards or power of pivot_lower perhaps, but without the requirements as well.

The first step is to make sure that class won't be an issue; because you have some strings in there, we need to convert all to character:

dw[-1] <- lapply(dw[-1], as.character)

After that, we can manually extract every two (non-ID) columns and combine with ID:

1` # ID q1 q2 # 1 A 10 6 # 2 B 12 5 # 3 C 20 7 # 4 D 22 8 #

It seems that you are not concerned with the column names (other than ID), and that they are all the same class. For this, we can "pivot" manually, without the safeguards or power of pivot_lower perhaps, but without the requirements as well.

The first step is to make sure that class won't be an issue; because you have some strings in there, we need to convert all to character:

dw[-1] <- lapply(dw[-1], as.character)

After that, we can manually extract every two (non-ID) columns and combine with ID:

2` # ID q1 q2 # 1 A 50 10 # 2 B 70 11 # 3 C 20 8 # 4 D 22 9 #

It seems that you are not concerned with the column names (other than ID), and that they are all the same class. For this, we can "pivot" manually, without the safeguards or power of pivot_lower perhaps, but without the requirements as well.

The first step is to make sure that class won't be an issue; because you have some strings in there, we need to convert all to character:

dw[-1] <- lapply(dw[-1], as.character)

After that, we can manually extract every two (non-ID) columns and combine with ID:

3` # ID q1 q2 # 1 A bA zA # 2 B bB zB # 3 C bC zC # 4 D bD zD

This can be easily combined with several methods, choose one of:

data.table::rbindlist(list_of_frames)
dplyr::bind_rows(list_of_frames)
do.call(rbind, list_of_frames)
#    ID q1 q2
# 1   A 10  6
# 2   B 12  5
# 3   C 20  7
# 4   D 22  8
# 5   A 50 10
# 6   B 70 11
# 7   C 20  8
# 8   D 22  9
# 9   A bA zA
# 10  B bB zB
# 11  C bC zC
# 12  D bD zD

Data

dw <- structure(list(ID = c("A", "B", "C", "D"), q1 = c("10", "12", "20", "22"), q2 = c("6", "5", "7", "8"), q3 = c("50", "70", "20", "22"), q4 = c("10", "11", "8", "9"), q5 = c("bA", "bB", "bC", "bD"), q6 = c("zA", "zB", "zC", "zD")), row.names = c(NA, -4L), class = "data.frame")
姐不稀罕 2025-01-29 03:16:35

另一个选项:

data.frame(ID = dw$ID,
           q1 = unlist(dw[,seq(2, ncol(dw), 2)], use.names = FALSE),
           q2 = unlist(dw[,seq(3, ncol(dw), 2)], use.names = FALSE))

使用数据:

dw <- structure(list(ID = c("A", "B", "C", "D"),
                     q1 = c(10L, 12L, 20L, 22L),
                     q2 = c(6L, 5L, 7L, 8L),
                     q3 = c(50L, 70L, 20L, 22L),
                     q4 = c(10L, 11L, 8L, 9L),
                     q5 = c("bA", "bB", "bC", "bD"),
                     q6 = c("cc", "dd", "ee", "ff"))
                , class = "data.frame", row.names = c(NA, -4L))

data.frame(ID = dw$ID,
           q1 = unlist(dw[,seq(2, ncol(dw), 2)], use.names = FALSE),
           q2 = unlist(dw[,seq(3, ncol(dw), 2)], use.names = FALSE))
#>    ID q1 q2
#> 1   A 10  6
#> 2   B 12  5
#> 3   C 20  7
#> 4   D 22  8
#> 5   A 50 10
#> 6   B 70 11
#> 7   C 20  8
#> 8   D 22  9
#> 9   A bA cc
#> 10  B bB dd
#> 11  C bC ee
#> 12  D bD ff

或更一般:

n <- 3L # operate on every 3 columns
data.frame(
  setNames(
    c(
      list(dw[,1]),
      lapply(
        2:(n + 1L),
        function(i) unlist(dw[,seq(i, ncol(dw), n)], TRUE, FALSE)
      )
    ),
    names(dw)[1:(n + 1L)]
  )
)

#>   ID q1 q2 q3
#> 1  A 10  6 50
#> 2  B 12  5 70
#> 3  C 20  7 20
#> 4  D 22  8 22
#> 5  A 10 bA cc
#> 6  B 11 bB dd
#> 7  C  8 bC ee
#> 8  D  9 bD ff

Another option:

data.frame(ID = dw$ID,
           q1 = unlist(dw[,seq(2, ncol(dw), 2)], use.names = FALSE),
           q2 = unlist(dw[,seq(3, ncol(dw), 2)], use.names = FALSE))

With data:

dw <- structure(list(ID = c("A", "B", "C", "D"),
                     q1 = c(10L, 12L, 20L, 22L),
                     q2 = c(6L, 5L, 7L, 8L),
                     q3 = c(50L, 70L, 20L, 22L),
                     q4 = c(10L, 11L, 8L, 9L),
                     q5 = c("bA", "bB", "bC", "bD"),
                     q6 = c("cc", "dd", "ee", "ff"))
                , class = "data.frame", row.names = c(NA, -4L))

data.frame(ID = dw$ID,
           q1 = unlist(dw[,seq(2, ncol(dw), 2)], use.names = FALSE),
           q2 = unlist(dw[,seq(3, ncol(dw), 2)], use.names = FALSE))
#>    ID q1 q2
#> 1   A 10  6
#> 2   B 12  5
#> 3   C 20  7
#> 4   D 22  8
#> 5   A 50 10
#> 6   B 70 11
#> 7   C 20  8
#> 8   D 22  9
#> 9   A bA cc
#> 10  B bB dd
#> 11  C bC ee
#> 12  D bD ff

Or more generally:

n <- 3L # operate on every 3 columns
data.frame(
  setNames(
    c(
      list(dw[,1]),
      lapply(
        2:(n + 1L),
        function(i) unlist(dw[,seq(i, ncol(dw), n)], TRUE, FALSE)
      )
    ),
    names(dw)[1:(n + 1L)]
  )
)

#>   ID q1 q2 q3
#> 1  A 10  6 50
#> 2  B 12  5 70
#> 3  C 20  7 20
#> 4  D 22  8 22
#> 5  A 10 bA cc
#> 6  B 11 bB dd
#> 7  C  8 bC ee
#> 8  D  9 bD ff
笑红尘 2025-01-29 03:16:35

Melt(...) data.table的方法允许列表融化列。使用dw来自 @r2evans答案:

library(data.table)
setDT(dw)
result <- melt(dw, measure.vars = list(seq(2, ncol(dw), 2), seq(3, ncol(dw), 2)))
result[, variable:=NULL]
result
##     ID value1 value2
##  1:  A     10      6
##  2:  B     12      5
##  3:  C     20      7
##  4:  D     22      8
##  5:  A     50     10
##  6:  B     70     11
##  7:  C     20      8
##  8:  D     22      9
##  9:  A     bA     zA
## 10:  B     bB     zB
## 11:  C     bC     zC
## 12:  D     bD     zD

Melt(...)引入列actible,该可以跟踪原始列的位置宽数据集。您似乎不在乎这一点,因此已将其删除。如果确实有不同的类(整数,字符)熔体(...)将通过警告来处理这一点。

The melt(...) method for data.table allows for melting groups of columns. Using dw from @r2evans answer:

library(data.table)
setDT(dw)
result <- melt(dw, measure.vars = list(seq(2, ncol(dw), 2), seq(3, ncol(dw), 2)))
result[, variable:=NULL]
result
##     ID value1 value2
##  1:  A     10      6
##  2:  B     12      5
##  3:  C     20      7
##  4:  D     22      8
##  5:  A     50     10
##  6:  B     70     11
##  7:  C     20      8
##  8:  D     22      9
##  9:  A     bA     zA
## 10:  B     bB     zB
## 11:  C     bC     zC
## 12:  D     bD     zD

melt(...) introduces a column variable which keeps track of the location of the original columns in the wide dataset. You don't seem to care about that so it's removed. If there are indeed different classes (integer, character) melt(...) will take care of that with a warning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文