如何依次遍历r data.frame中的每一行和每一列?
我有一些调查数据是通过谷歌的表格输入的。谷歌生成了一个电子表格,上面有回复,但我需要做的是将这些数据分割成单独的回复,这样人们就可以像在博客或其他地方发表的采访一样阅读这些数据 假设我有这样的东西:如何依次遍历r data.frame中的每一行和每一列?,r,loops,dataframe,R,Loops,Dataframe,我有一些调查数据是通过谷歌的表格输入的。谷歌生成了一个电子表格,上面有回复,但我需要做的是将这些数据分割成单独的回复,这样人们就可以像在博客或其他地方发表的采访一样阅读这些数据 假设我有这样的东西: 1st Question 2nd Question 3rd Question "Response1 q1" "Response1 q2" "Response1 q3" "Response2 q1" "Response2 q2" "Response2 q3"
1st Question 2nd Question 3rd Question
"Response1 q1" "Response1 q2" "Response1 q3"
"Response2 q1" "Response2 q2" "Response2 q3"
"Response3 q1" "Response3 q2" "Response3 q3"
1st Question
-------
Response1 q1
2nd Question
-------
Response1 q2
3rd Question
-------
Response1 q3
qdata = structure(list(Q1.text = c("1r.text", "2r.text", "3r.text"),
Q2.text = c("1r.text", "2r.text", "3r.text"), Q3.text = c("1r.text",
"2r.text", "3r.text"), Q4.text = c("1r.text", "2r.text",
"3r.text")), .Names = c("Q1.text", "Q2.text", "Q3.text",
"Q4.text"), class = "data.frame", row.names = c(NA, -3L))
其中第一行(列标题)是问题,每行都填写了对这些问题的回答。我想要制作的是这样的东西:
1st Question 2nd Question 3rd Question
"Response1 q1" "Response1 q2" "Response1 q3"
"Response2 q1" "Response2 q2" "Response2 q3"
"Response3 q1" "Response3 q2" "Response3 q3"
1st Question
-------
Response1 q1
2nd Question
-------
Response1 q2
3rd Question
-------
Response1 q3
qdata = structure(list(Q1.text = c("1r.text", "2r.text", "3r.text"),
Q2.text = c("1r.text", "2r.text", "3r.text"), Q3.text = c("1r.text",
"2r.text", "3r.text"), Q4.text = c("1r.text", "2r.text",
"3r.text")), .Names = c("Q1.text", "Q2.text", "Q3.text",
"Q4.text"), class = "data.frame", row.names = c(NA, -3L))
基本上,对于每个受访者,我想制作一个单独的文件,以线性方式显示他们的问题回答
我已经给出了我试图解决的问题的细节,以防我的特殊情况有一个捷径,但一般来说,如果你在R中有一个data.frame,无论出于什么原因,你都需要逐行遍历,然后逐列遍历,如果只为循环编写一些代码,您将如何完成这一任务?这是使用循环执行此操作的标准方法:
for(i in 1:nrow(df)){ #traverse rows
for(ii in 1:ncol(df)){ #traverse cols
#do whatever
}
}
其中,
df
是您的数据帧假设您的数据位于数据帧中(使用字符串,而不是因子),如下所示:
1st Question 2nd Question 3rd Question
"Response1 q1" "Response1 q2" "Response1 q3"
"Response2 q1" "Response2 q2" "Response2 q3"
"Response3 q1" "Response3 q2" "Response3 q3"
1st Question
-------
Response1 q1
2nd Question
-------
Response1 q2
3rd Question
-------
Response1 q3
qdata = structure(list(Q1.text = c("1r.text", "2r.text", "3r.text"),
Q2.text = c("1r.text", "2r.text", "3r.text"), Q3.text = c("1r.text",
"2r.text", "3r.text"), Q4.text = c("1r.text", "2r.text",
"3r.text")), .Names = c("Q1.text", "Q2.text", "Q3.text",
"Q4.text"), class = "data.frame", row.names = c(NA, -3L))
(下次,请与dput
共享数据,使其结构易于复制。)
我会选择矢量化的解决方案。在这里,我转换为矩阵,然后将列名粘贴到条目中,用新行(“\n”
)和破折号分隔,如您的示例所示
qdata.m = as.matrix(qdata)
# Next, we take advantage of "recycling" of the column names,
# pasting them to the matrix values with a newline "\n" separator.
qdata.m = paste(colnames(qdata.m), "-------", t(qdata.m), sep = "\n")
# Note that matrices are normally used column-wise, so I transpose t()
# to make it row-wise instead.
# cat is good for putting text into a file. We'll separate each
# element with two line breaks.
cat(qdata.m, sep = "\n\n")
# Q1.text
# -------
# 1r.text
#
# Q2.text
# -------
# 1r.text
#
# Q3.text
# -------
# 1r.text
# etc.
在此处使用cat
的优点之一是它可以直接打印到文件(或者您可以先打开与sink
的连接--有关详细信息,请参阅其相关帮助页)
在更一般的情况下,如果需要逐行、逐列地执行,可以使用嵌套for循环来执行。在这一点上,您似乎还没有真正使用数据帧结构,因此您可以使用unlist()
将其转换为向量实际上,在这种情况下,这可能比我上面所做的更容易:
qvect = unlist(qdata)
# pasting much as above, with an order() to sort by the text
# (the order step may take more care with non-dummy text that isn't
# alphabetical)
qvect = paste(names(qvect), "--------", qvect, sep = "\n")[order(qvect)]
然后你可以继续上面的
cat
。我确实想过这样做(现在是这样,正如我们所说的),但它看起来太不自然了。几乎每次我写一个循环在R中做一件事时,都会有其他人出现,然后说“不,矢量化”或“使用*apply”函数或其他什么。所以我想我会问。很抱歉,这个问题似乎不清楚(但我向你保证目的是现实的)。我选择不共享我正在使用的真实数据,因为它是专有的,我尽我所能在一个框中键入文本来提供一个示例。我试图更新这个问题,也许现在更清楚了?不清楚数据是存储在矩阵还是数据框中,如果框中的第一行是列名还是仅仅是数据的第一行,如果是数据框,数据类型是因子还是字符。对于输出,您真的只想将输出打印到控制台吗?它应该存储在R对象中(作为单个字符串、字符向量或列表?)并写入文本文件吗?不共享真实数据很好,但以一种使底层结构清晰的方式共享假数据很好dput(伪数据)
是创建可复制粘贴的R对象的一种很好的方法,这就是我在回答中为qdata
所做的。谢谢你的提示。有时很难判断哪些细节会增加清晰度,哪些细节会使我更难看到我试图解决的问题,特别是因为我在这方面有点新手。不幸的是,学习如何提出好问题是这种东西学习曲线的一部分。。。。但是我删除了刺耳的第一行,对不起,如果我冒犯了;)谢谢你把事情弄清楚!