打印数据帧列表中的第N行 [英] Print the Nth Row in a List of Data Frames

查看:137
本文介绍了打印数据帧列表中的第N行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在清理R中的几个excel文件。不幸的是它的尺寸,行和列都不相等。目前,我将每个excel表存储为列表中的数据框。我知道如何通过发出以下命令在列表中打印第一个数据帧的第4行:

  df.list1 [[ 1]] [4,] 

或一系列这样的行:

  df.list1 [[1]] [1:10,] 

我的问题是:如何为列表中的每个数据框打印特定行?换句话说:

  df.list1 [[i]] [4,] 

df.list1 其中有30个数据框,但我的其他 df.lists 有超过140个数据帧,我正在寻找提取他们的行。我想要将几个数据帧中的特定位置存储到新的列表中。我想这个解决方案可能涉及 lapply



此外,有没有办法在每个数据中提取行基于条件的列表框架?例如,对于列表df.list1中的所有30个数据帧,如果该值等于公寓或其他一些字符串,则提取该行。



欣赏您的帮助,请让我知道,如果我可以帮助澄清我的问题。

解决方案

你也可以直接 lapply 提取函数@Justin建议,例如:

 #包含10的列表示例数据数据框:
test< - replicate(10,data.frame(a = 1:10),simplified = FALSE)

#提取每一行的第四行 - 设置drop = FALSE意味着即使只需要返回一个向量/列,也会返回
#数据帧。
lapply(test,[,4,,drop = FALSE)

格式是:

  lapply(listname,[,rows.to.return,cols.to.return,drop = FALSE) 

#该示例仅从每个数据框返回第四行
#[[1]]
#a
#4 4

#[[2]]
#a
#4 4
#等...

要在基于条件完成提取时进行推广,您必须将其更改一些,如下面的示例提取所有行,其中 a 每个 data.frame > 4 。在这种情况下,使用匿名功能可能是最清晰的方法,例如:



pre $ lapply(test,function(x)with(x,x [a> 4,,drop = FALSE]))

#[[ 1]]
#a
#5 5
#6 6
#7 7
#8 8
#9 9
#10 10
#等...


I am cleaning several excel files in R. They unfortunately are of unequal dimensions, rows and columns. Currently I am storing each excel sheet as a data frame in a list. I know how to print the 4th row of the first data frame in a list by issuing this command:

df.list1[[1]][4,]

Or a range of rows like this:

df.list1[[1]][1:10,]

My question is: How do I print a particular row for every data frame in the list? In other words:

df.list1[[i]][4,]

df.list1 has 30 data frames in it, but my other df.lists have over 140 data frames that I am looking to extract their rows. I'd like to be able to store particular locations across several data frames into a new list. I'm thinking the solution might involve lapply.

Furthermore, is there a way to extract rows in every data frame in a list based on a condition? For example, for all 30 data frames in the list df.list1, extract the row if the value is equal to "Apartment" or some other string of characters.

Appreciate your help, please let me know if I can help clarify my problem.

解决方案

You could also just directly lapply the extraction function @Justin suggests, e.g.:

# example data of a list containing 10 data frames:
test <- replicate(10,data.frame(a=1:10),simplify=FALSE)

# extract the fourth row of each one - setting drop=FALSE means you get a
# data frame returned even if only one vector/column needs to be returned.
lapply(test,"[",4,,drop=FALSE)

The format is:

lapply(listname,"[",rows.to.return,cols.to.return,drop=FALSE)

# the example returns the fourth row only from each data frame
#[[1]]
#  a
#4 4
# 
#[[2]]
#  a
#4 4
# etc...

To generalise this when you are completing an extraction based on a condition, you would have to change it up a little to something like the below example extracting all rows where a in each data.frame is >4. In this case, using an anonymous function is probably the clearest method, e.g.:

lapply(test, function(x) with(x,x[a>4,,drop=FALSE]) )

#[[1]]
#    a
#5   5
#6   6
#7   7
#8   8
#9   9
#10 10
# etc...

这篇关于打印数据帧列表中的第N行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆