For循环将一个大数据框(30列)分成几个较小的数据框(3列)

发布于 2025-01-16 12:47:21 字数 473 浏览 0 评论 0原文

第一次在这里写问题,请不要太严厉。我有一个名为“Time”列、一个“Well”列和 28 列合并的大型 data.frame,其中每列存储来自板测量的数据(因此 28 个板)df 称为合并。现在我想创建一个“for 循环”,它使用“Time”、“Well”列以及从板 1 或第 3 列开始直到最后一个板(28 或第 30 列)的板的测量值来创建新的 dfs。 问题是,我不知道如何在循环中调整新 df 的名称,以便它创建 28 个不同的新 df,仅包含 1 个板的信息,而不是仅仅覆盖新 df 并仅存储最后一个板的信息盘子。 初级循环函数。 如果您需要更多信息,请随时询问,我将尽力提供信息。

提前谢谢大家!

罗杰

first time writing a question here so please don't be too harsh. I have a large data.frame called merged with a "Time" column, a "Well" column and 28 columns where each column stores data from measurements of a plate (so 28 plates)df called merged. Now I want to create a "for loop" that creates new dfs with the columns "Time", "Well" and the measurements of a plate starting with plate 1 or column 3 up until the last plate (28 or column 30).
The problem is, I have no idea how I can adjust the name of the new df in the loop so it creates 28 different new dfs with the information of only 1 plate instead of just overwriting the new df and storing only the information of the last plate. beginner loop function.
if you need more information, feel free to ask and I will try to deliver the information to the best of my ability.

Thank you all in advance!

Roger

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

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

发布评论

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

评论(2

野稚 2025-01-23 12:47:21

这是一个精心设计的例子。我首先创建一个与您的数据类似的 data.frame。然后,我使用 tidyr 包将宽格式转换为长格式。 (还有其他方法可以做到这一点)。

使用长格式,可以轻松地通过板标识符选择所需的数据。

#----------------------
# Cook up a data.frame
#----------------------
# 30 sequential dates
dates = seq.Date(as.Date("2022-03-01"), as.Date("2022-03-30"), 1)
# 50 wells 
wells <- lapply(LETTERS[1:5], function(l) {paste0(l, seq(1, 10))})
wells <- unlist(wells)
# Create a data.frame
wells_data <- data.frame(expand.grid(dates, wells))
names(wells_data) <- c("Dates", "Wells")

# 30 columns of artificial data
for (i in 1:30) {
  new_data <- data.frame(runif(1:nrow(wells_data)))
  names(new_data) <- paste0("Plate", i)
  wells_data <- cbind(wells_data, new_data)
}
head(wells_data)
           Dates Wells     Plate1    Plate2    Plate3     Plate4     Plate5
1 2022-03-01    A1 0.20418463 0.5932133 0.7070428 0.04231371 0.25872767
2 2022-03-02    A1 0.95218240 0.1114270 0.3763757 0.22992064 0.05632674
3 2022-03-03    A1 0.07162576 0.9902931 0.1437405 0.40102327 0.56432590
4 2022-03-04    A1 0.17148644 0.1849485 0.2062618 0.45908182 0.44657831
5 2022-03-05    A1 0.11334931 0.4820294 0.1663636 0.87436576 0.60177308
6 2022-03-06    A1 0.13949741 0.7862085 0.6162253 0.50698110 0.75309069
      Plate6     Plate7      Plate8    Plate9    Plate10    Plate11   Plate12
1 0.77206623 0.45816279 0.002027475 0.3821823 0.30170925 0.08730046 0.7638708
2 0.31140577 0.39479768 0.919386005 0.2369556 0.33105790 0.86560846 0.9464049
3 0.36804632 0.30644346 0.782938605 0.3723977 0.21561693 0.14770805 0.7371391
4 0.07265802 0.68454399 0.916244462 0.7688442 0.36590464 0.42293563 0.8448824
5 0.59587190 0.78073586 0.338200076 0.3895508 0.61586528 0.47494553 0.8315232
6 0.41189998 0.06666752 0.721342234 0.5130501 0.06648771 0.61675408 0.9384815
# ...more columns...

#----------------------
# Now convert from wide to long
# and split by plate identifier
#----------------------
library(tidyr)
wells_data <- pivot_longer(wells_data,
                           cols=(3:ncol(wells_data)),
                           names_to="Plate",
                           values_to="measurement")
head(wells_data)
# A tibble: 6 × 4
  Dates      Wells Plate  measurement
  <date>     <fct> <chr>        <dbl>
1 2022-03-01 A1    Plate1      0.204 
2 2022-03-01 A1    Plate2      0.593 
3 2022-03-01 A1    Plate3      0.707 
4 2022-03-01 A1    Plate4      0.0423
5 2022-03-01 A1    Plate5      0.259 
6 2022-03-01 A1    Plate6      0.772 

# Now it's easy to select out each Plate:
plates = unique(wells_data$Plate)
lapply(plates, function(p) {
         subset = wells_data[wells_data$Plate == p,]
         # Do whatever you want with this subset
         print(paste("Mean for Plate", p, ":",
                    mean(subset$measurement)))
         
})

希望这可以帮助您前进。

Here's a cooked up example. I begin by creating a data.frame that is similar to your data. Then I convert from wide format to long, using the tidyr package. (There are other ways to do this).

With the long format, it's then easy to select out the data you want by Plate identifier.

#----------------------
# Cook up a data.frame
#----------------------
# 30 sequential dates
dates = seq.Date(as.Date("2022-03-01"), as.Date("2022-03-30"), 1)
# 50 wells 
wells <- lapply(LETTERS[1:5], function(l) {paste0(l, seq(1, 10))})
wells <- unlist(wells)
# Create a data.frame
wells_data <- data.frame(expand.grid(dates, wells))
names(wells_data) <- c("Dates", "Wells")

# 30 columns of artificial data
for (i in 1:30) {
  new_data <- data.frame(runif(1:nrow(wells_data)))
  names(new_data) <- paste0("Plate", i)
  wells_data <- cbind(wells_data, new_data)
}
head(wells_data)
           Dates Wells     Plate1    Plate2    Plate3     Plate4     Plate5
1 2022-03-01    A1 0.20418463 0.5932133 0.7070428 0.04231371 0.25872767
2 2022-03-02    A1 0.95218240 0.1114270 0.3763757 0.22992064 0.05632674
3 2022-03-03    A1 0.07162576 0.9902931 0.1437405 0.40102327 0.56432590
4 2022-03-04    A1 0.17148644 0.1849485 0.2062618 0.45908182 0.44657831
5 2022-03-05    A1 0.11334931 0.4820294 0.1663636 0.87436576 0.60177308
6 2022-03-06    A1 0.13949741 0.7862085 0.6162253 0.50698110 0.75309069
      Plate6     Plate7      Plate8    Plate9    Plate10    Plate11   Plate12
1 0.77206623 0.45816279 0.002027475 0.3821823 0.30170925 0.08730046 0.7638708
2 0.31140577 0.39479768 0.919386005 0.2369556 0.33105790 0.86560846 0.9464049
3 0.36804632 0.30644346 0.782938605 0.3723977 0.21561693 0.14770805 0.7371391
4 0.07265802 0.68454399 0.916244462 0.7688442 0.36590464 0.42293563 0.8448824
5 0.59587190 0.78073586 0.338200076 0.3895508 0.61586528 0.47494553 0.8315232
6 0.41189998 0.06666752 0.721342234 0.5130501 0.06648771 0.61675408 0.9384815
# ...more columns...

#----------------------
# Now convert from wide to long
# and split by plate identifier
#----------------------
library(tidyr)
wells_data <- pivot_longer(wells_data,
                           cols=(3:ncol(wells_data)),
                           names_to="Plate",
                           values_to="measurement")
head(wells_data)
# A tibble: 6 × 4
  Dates      Wells Plate  measurement
  <date>     <fct> <chr>        <dbl>
1 2022-03-01 A1    Plate1      0.204 
2 2022-03-01 A1    Plate2      0.593 
3 2022-03-01 A1    Plate3      0.707 
4 2022-03-01 A1    Plate4      0.0423
5 2022-03-01 A1    Plate5      0.259 
6 2022-03-01 A1    Plate6      0.772 

# Now it's easy to select out each Plate:
plates = unique(wells_data$Plate)
lapply(plates, function(p) {
         subset = wells_data[wells_data$Plate == p,]
         # Do whatever you want with this subset
         print(paste("Mean for Plate", p, ":",
                    mean(subset$measurement)))
         
})

Hope this might help to get you going.

无力看清 2025-01-23 12:47:21

避免用许多结构相似、独立的数据框淹没您的全局环境。考虑使用 lapplysapply(对于名称)构建许多相关元素的单个列表,以构建子集数据框的列表。如下所示,如果保存在较大的列表中,则不会丢失 data.frame 的功能:

# RETRIEVE ALL V-STARTING COLUMN NAMES
v_cols <- colnames(merged_df)[grep("^V", colnames(merged_df))]

# NAMED LIST OF PLATE SUBSETTED DATA FRAMES
plate_measurements_list <- sapply(
    v_cols, 
    function(i) merged_df[,c("Time", "Well", col)],
    simplify = FALSE
)


# ACCESS AND USE EACH DATA FRAME
head(plate_measurements_list$V1)
tail(plate_measurements_list$V2)
summary(plate_measurements_list$V3)
...
str(plate_measurements_list$V28)

Avoid flooding your global environment with many structurally similar, separate data frames. Consider building a single list of many related elements using lapply or sapply (for names) to build a list of subset data frames. As shown below, you lose no functionality of data.frames if saved in a larger list:

# RETRIEVE ALL V-STARTING COLUMN NAMES
v_cols <- colnames(merged_df)[grep("^V", colnames(merged_df))]

# NAMED LIST OF PLATE SUBSETTED DATA FRAMES
plate_measurements_list <- sapply(
    v_cols, 
    function(i) merged_df[,c("Time", "Well", col)],
    simplify = FALSE
)


# ACCESS AND USE EACH DATA FRAME
head(plate_measurements_list$V1)
tail(plate_measurements_list$V2)
summary(plate_measurements_list$V3)
...
str(plate_measurements_list$V28)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文