允许将 .cvs 数据表作为单独的列和行数据读取

Posted

技术标签:

【中文标题】允许将 .cvs 数据表作为单独的列和行数据读取【英文标题】:Enable reading of .cvs data table as a separated columns and rows data 【发布时间】:2021-12-29 17:14:24 【问题描述】:

我有这个数据集

> dput(head(Filter, 50))
structure(list(X..Subject...Change...Ndist...correct_response...test_response...correct...response_time...Block...Attention_focus...Awareness_attention...trial...Age...Online_hrs...Sex...MM_hrs...MM_TV...MM_IM...MM_SM...MMI...MMH...MMS...MMI_grp...MMS_grp...MMH_grp...MPI...screen_width...screen_height...backend...location. = c("1,1,\"yes\",6,\"left\",\"right\",0,976,1,1,3,1,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"2,1,\"yes\",6,\"left\",\"left\",1,807,1,1,3,2,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"3,1,\"yes\",2,\"left\",\"left\",1,622,1,1,3,3,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"4,1,\"no\",2,\"right\",\"right\",1,710,1,1,3,4,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"5,1,\"no\",4,\"right\",\"right\",1,598,1,1,3,5,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"6,1,\"yes\",0,\"left\",\"left\",1,574,1,1,3,6,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"7,1,\"no\",6,\"right\",\"right\",1,791,1,1,3,7,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"8,1,\"no\",4,\"right\",\"right\",1,622,1,1,3,8,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"9,1,\"yes\",4,\"left\",\"left\",1,766,1,1,3,9,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"10,1,\"yes\",0,\"left\",\"left\",1,668,1,1,3,10,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"11,1,\"no\",2,\"right\",\"left\",0,1246,1,1,3,11,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"12,1,\"yes\",4,\"left\",\"left\",1,992,1,1,3,12,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"13,1,\"no\",0,\"right\",\"right\",1,797,1,1,3,13,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"14,1,\"no\",0,\"right\",\"right\",1,878,1,1,3,14,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"15,1,\"yes\",2,\"left\",\"left\",1,1191,1,1,3,15,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"16,1,\"no\",6,\"right\",\"right\",1,990,1,1,3,16,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"17,1,\"yes\",0,\"left\",\"left\",1,1666,2,3,1,1,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"18,1,\"no\",6,\"right\",\"right\",1,789,2,3,1,2,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"19,1,\"yes\",6,\"left\",\"left\",1,758,2,3,1,3,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"20,1,\"yes\",2,\"left\",\"left\",1,654,2,3,1,4,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"21,1,\"no\",4,\"right\",\"right\",1,726,2,3,1,5,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"22,1,\"yes\",6,\"left\",\"left\",1,1408,2,3,1,6,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"23,1,\"yes\",4,\"left\",\"left\",1,718,2,3,1,7,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"24,1,\"yes\",4,\"left\",\"left\",1,766,2,3,1,8,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"25,1,\"no\",2,\"right\",\"right\",1,750,2,3,1,9,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"26,1,\"no\",0,\"right\",\"right\",1,649,2,3,1,10,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"27,1,\"no\",0,\"right\",\"right\",1,656,2,3,1,11,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"28,1,\"no\",6,\"right\",\"right\",1,1418,2,3,1,12,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"29,1,\"yes\",2,\"left\",\"left\",1,671,2,3,1,13,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"30,1,\"no\",2,\"right\",\"left\",0,809,2,3,1,14,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"31,1,\"yes\",0,\"left\",\"left\",1,767,2,3,1,15,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"32,1,\"no\",4,\"right\",\"right\",1,649,2,3,1,16,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"33,1,\"yes\",4,\"left\",\"left\",1,1038,3,2,5,1,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"34,1,\"no\",6,\"right\",\"right\",1,820,3,2,5,2,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"35,1,\"yes\",2,\"left\",\"left\",1,654,3,2,5,3,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"36,1,\"no\",0,\"right\",\"right\",1,756,3,2,5,4,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"37,1,\"no\",0,\"right\",\"right\",1,1087,3,2,5,5,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"38,1,\"yes\",2,\"left\",\"left\",1,773,3,2,5,6,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"39,1,\"no\",2,\"right\",\"right\",1,702,3,2,5,7,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"40,1,\"yes\",0,\"left\",\"left\",1,926,3,2,5,8,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"41,1,\"no\",4,\"right\",\"right\",1,838,3,2,5,9,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"42,1,\"yes\",6,\"left\",\"right\",0,1702,3,2,5,10,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"43,1,\"yes\",4,\"left\",\"left\",1,822,3,2,5,11,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"44,1,\"yes\",6,\"left\",\"right\",0,1030,3,2,5,12,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"45,1,\"no\",6,\"right\",\"right\",1,518,3,2,5,13,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"46,1,\"yes\",0,\"left\",\"left\",1,599,3,2,5,14,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"47,1,\"no\",4,\"right\",\"right\",1,925,3,2,5,15,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"48,1,\"no\",2,\"right\",\"right\",1,629,3,2,5,16,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"49,1,\"no\",2,\"right\",\"right\",1,1077,4,1,3,1,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\"", 
"50,1,\"yes\",2,\"left\",\"left\",1,917,4,1,3,2,24,2,0,3.5,1.666666667,2,1.666666667,0.711428571,2.49,1.777777778,\"LMM\",\"LMM\",\"LMM\",51,1024,768,\"xpyriment\",\"lab\""
)), row.names = c(NA, 50L), class = "data.frame")
> 

如您所见,它是 .csv 格式。顺便说一句,尽管我寻找将其作为分隔的列和行元素来阅读,但我无法找到启用这种阅读选项的代码。有谁知道如何启用它?

如果有兴趣,这是我在其中找到文件的原始来源

https://mfr.osf.io/render?url=https://osf.io/j79ne/?direct%26mode=render%26action=download%26mode=render

运行代码Filter <- read.csv("filter_raw.csv", sep = ',')后,我得到一个这样的数据集

无法执行研究人员在此处上传的整个代码https://osf.io/nkdw5/

【问题讨论】:

嗨!我不确定“作为分隔的列和行数据”是什么意思。你能分享一个关于你得到什么输出以及你想要实现什么输出的小例子吗? 嗨,这是一个 .csv 文档。逗号应该是分隔符,但由于我已经非常努力地使用分离单元变量来获得它,所以我在这里发布了这个问题。它应该使用不同单元格中的每个值正常重新整形。 我不确定您或我是否使用了错误的链接,但 pth = "https://osf.io/j79ne/download"; dat = read.csv(pth) 下载了一个 csv 罚款(我从转到 osf.io/nkdw5 获得了链接,然后单击数据部分中的 filter_raw.csv ,然后从下载按钮(页面右上角)复制链接) 谢谢。这是我一直在寻找的正确答案。 我应该在下面写下它对我有用的答案吗?一个人? 【参考方案1】:

问题中使用了错误的链接。正确的链接可以通过转到https://osf.io/nkdw5/,然后单击Data 部分中的filter_raw.csv,然后从下载按钮(页面右上角)复制链接来找到。

这里是效果更好的解决方案。

pth <- "https://osf.io/j79ne/download"
dat <- read.csv(pth)

【讨论】:

我认为您应该将此标记为解决方案;另一个答案是针对 python 而不是 R,并且没有详细说明如何从网页中获取链接,这很困难。【参考方案2】:

如果使用 pandas 会更方便

import pandas as pd

df = pd.read_csv('data.csv')

有关熊猫的更多信息,请参阅:- https://www.w3schools.com/python/pandas/default.asp

【讨论】:

以上是关于允许将 .cvs 数据表作为单独的列和行数据读取的主要内容,如果未能解决你的问题,请参考以下文章

如何将数据从 python 列表中的列和行写入 csv 文件?

VB 如何读取 Excel 所有列和行

Pandas列表的列,通过迭代(选择)三列的每个列表元素作为新列和行来创建多列[重复]

Pandas:DataFrame数据的更改插入新增的列和行

如何将 Pandas DataFrame 的列和行子集转换为 numpy 数组?

删除熊猫数据框中包含特定值的列和行[重复]