【问题标题】:looping though data frames for na's for a beginner为初学者循环遍历 nans 的数据帧
【发布时间】:2021-02-15 16:20:25
【问题描述】:

我是初学者,我的环境中有 42 个数据框,每个数据框都有多个带有 na 的列。

我可以遍历每个数据帧并删除 na 吗?

代码会是什么样子?

附上2张环境图和每个数据框的样子,

全球

数据框

【问题讨论】:

  • 请通过说明您使用的是哪种技术堆栈来澄清问题。当您仅提及通用“数据框”时,很难回答您的问题。
  • 我相信这两张照片显示了我的要求。技术是测力板和运动相机,但我认为这并不重要,我只需要移除 na。谢谢你的问题。我很感激。

标签: r for-loop signal-processing na


【解决方案1】:

您最好先将所有数据框读入一个列表。如果您的所有数据框都是单独的对象,您可以这样做:

# Character vector of all objects in the current environment 
# (including all the data frames)
dfs = ls()

# Filter to keep only names of the data frames
dfs = dfs[grep("df_.*", dfs)]

# Add names (so that the elements of the list we create below will
# be named with the name of the source data frame)
names(dfs) = dfs

# Return a list where each element is a data frame.
# In each data frame, all rows with at least one NA will be removed.
df.na.remove = lapply(dfs, function(x) na.omit(get(x)))

# Or this
df.na.remove = lapply(dfs, function(x) {
  d = get(x)
  d[complete.cases(d), ]
})

您现在有一个包含所有数据框的列表,但如果行有任何 NA 值,则将其删除。

如果你想从全局环境中移除原始数据框,你可以这样做:

rm(list=dfs)

如果您想首先将所有数据读入一个列表,这里有一些代码。下面的示例切换到tidyverse 函数。

library(tidyverse)

# Save two data frames, just to have something to work with
write_csv(mtcars[1:5, ], "df_1.csv")
write_csv(mtcars[6:10, ], "df_2.csv")

# Create a character vector with names of our data files
f = list.files(pattern="df_.*") %>% set_names()

# Read each data frame into a single list
d1 = map(f, read_csv)

# Remove NA values
d1 = map(d1, na.omit)

作为另一种选择,您可以读入所有数据文件,删除任何具有至少一个 NA 值的行,并将所有数据帧堆叠成一个数据帧,全部在一个操作中完成:

d = map_df(f, ~ {
  x = read_csv(.x)
  na.omit(x)
  }, .id="source")

【讨论】:

    猜你喜欢
    • 2016-04-25
    • 2021-11-22
    • 2017-12-14
    • 2021-12-15
    • 1970-01-01
    • 2017-03-01
    • 2018-11-13
    • 2021-05-16
    相关资源
    最近更新 更多