在 R 中导入和分析非矩形 .csv 文件

2024-02-11

我将从 Mathematica 迁移到 R,在导入过程中我不需要预测数据结构,特别是我不需要在导入之前预测数据的矩形性。

我有很多文件.csv文件格式如下:

tasty,chicken,cinnamon
not_tasty,butter,pepper,onion,cardamom,cayenne
tasty,olive_oil,pepper
okay,olive_oil,onion,potato,black_pepper
not_tasty,tomato,fenugreek,pepper,onion,potato
tasty,butter,cheese,wheat,ham

行具有不同的长度,并且仅包含字符串。

在R中,我应该如何解决这个问题?

你尝试过什么?

我尝试过read.table:

dataImport <- read.table("data.csv", header = FALSE)
class(dataImport)
##[1] "data.frame"
dim(dataImport)
##[1] 6   1
dataImport[1]
##[1] tasty,chicken,cinnamon
##6 Levels: ...

我从文档中将其解释为一个单一的列,每个成分列表作为一个不同的行。我可以如下提取前三行,每一行都是class factor但似乎包含比我预期更多的数据:

dataImport[c(1,2,3),1]
## my rows
rowOne <- dataImport[c(1),1];
class(rowOne)
## "factor"
rowOne
## [1] tasty,chicken,cinnamon
## 6 Levels: not_tasty,butter,cheese [...]

这是我目前所追求的这个问题,我将不胜感激关于适用性的建议read.table对于这个数据结构。

我的目标是按每行的第一个元素对数据进行分组,并分析每种类型食谱之间的差异。如果它有助于影响数据结构建议,在 Mathematica 中我会执行以下操作:

dataImport=Import["data.csv"];
tasty = Cases[dataImport, {"tasty", ingr__} :> {ingr}]

回答讨论

@G.Grothendieck 提供了一个使用的解决方案read.table以及后续处理使用reshape2包 - 这似乎非常有用,我稍后会进行调查。这里的一般建议解决了我的问题,因此接受。

@MrFlick 使用的建议tm包对于以后的分析很有用DataframeSource


读表 Try read.table with fill=TRUE:

d1 <- read.table("data.csv", sep = ",", as.is = TRUE, fill = TRUE)

giving:

> d1
         V1        V2        V3     V4           V5      V6
1     tasty   chicken  cinnamon                            
2 not_tasty    butter    pepper  onion     cardamom cayenne
3     tasty olive_oil    pepper                            
4      okay olive_oil     onion potato black_pepper        
5 not_tasty    tomato fenugreek pepper        onion  potato
6     tasty    butter    cheese  wheat          ham   

带有 NA 的 read.table

或者用 NA 值填充空单元格添加na.strings = "" :

d2 <- read.table("data.csv", sep = ",", as.is = TRUE, fill = TRUE, na.strings = "")

giving:

> d2
         V1        V2        V3     V4           V5      V6
1     tasty   chicken  cinnamon   <NA>         <NA>    <NA>
2 not_tasty    butter    pepper  onion     cardamom cayenne
3     tasty olive_oil    pepper   <NA>         <NA>    <NA>
4      okay olive_oil     onion potato black_pepper    <NA>
5 not_tasty    tomato fenugreek pepper        onion  potato
6     tasty    butter    cheese  wheat          ham    <NA>

长表

如果您想要长格式:

library(reshape2)
long <- na.omit(melt(d2, id.var = c("id", "V1"))[-3])
long <- long[order(long$id), ]

giving:

> long
   id        V1        value
1   1     tasty      chicken
7   1     tasty     cinnamon
2   2 not_tasty       butter
8   2 not_tasty       pepper
14  2 not_tasty        onion
20  2 not_tasty     cardamom
26  2 not_tasty      cayenne
3   3     tasty    olive_oil
9   3     tasty       pepper
4   4      okay    olive_oil
10  4      okay        onion
16  4      okay       potato
22  4      okay black_pepper
5   5 not_tasty       tomato
11  5 not_tasty    fenugreek
17  5 not_tasty       pepper
23  5 not_tasty        onion
29  5 not_tasty       potato
6   6     tasty       butter
12  6     tasty       cheese
18  6     tasty        wheat
24  6     tasty          ham

宽格式 0/1 二进制变量

要将变量部分表示为 0/1 二进制变量,请尝试以下操作:

wide <- cast(id + V1 ~ value, data = long)
wide[-(1:2)] <- 0 + !is.na(wide[-(1:2)])

给出这个:

数据框中的列表

不同的表示形式是数据框中的以下列表,以便ag$value是字符向量列表:

ag <- aggregate(value ~., transform(long, value = as.character(value)), c)
ag <- ag[order(ag$id), ]

giving:

> ag
  id        V1                                    value
4  1     tasty                        chicken, cinnamon
1  2 not_tasty butter, pepper, onion, cardamom, cayenne
5  3     tasty                        olive_oil, pepper
3  4      okay   olive_oil, onion, potato, black_pepper
2  5 not_tasty tomato, fenugreek, pepper, onion, potato
6  6     tasty               butter, cheese, wheat, ham

> str(ag)
'data.frame':   6 obs. of  3 variables:
 $ id   : int  1 2 3 4 5 6
 $ V1   : chr  "tasty" "not_tasty" "tasty" "okay" ...
 $ value:List of 6
  ..$ 15: chr  "chicken" "cinnamon"
  ..$ 1 : chr  "butter" "pepper" "onion" "cardamom" ...
  ..$ 17: chr  "olive_oil" "pepper"
  ..$ 11: chr  "olive_oil" "onion" "potato" "black_pepper"
  ..$ 6 : chr  "tomato" "fenugreek" "pepper" "onion" ...
  ..$ 19: chr  "butter" "cheese" "wheat" "ham"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 R 中导入和分析非矩形 .csv 文件 的相关文章

随机推荐

  • URL 的锚点部分是否发送到 Web 服务器?

    比如说 有一个网址 http www example com hello 会不会 hello根据标准 是否将东西发送到网络服务器 现代浏览器如何运作 这个问题的答案与以下问题的答案类似检索 ASP NET URL 中的锚链接 https s
  • clojure 应用程序使用 eval 泄漏内存

    我的 clojure 应用程序评估单独定义的代码 edn文件在运行时 即如果 edn文件更改后 所包含的函数定义将重新加载到原子中 该原子不断用于计算 如果不受限制的话 应用程序似乎会在一段时间后填满 JVM 的元空间 无限制 堆空间使用情
  • IE问题:它可以处理多少个CSS包含?

    我在主题 Drupal 时遇到了一个奇怪的行为 我打开了一些模块 向页面添加了 5 到 10 个链接标签 虽然这些新的样式表被添加到 Firefox 的级联中 但在 IE8 中 通过添加这些样式表 浏览器会从层次结构中丢弃之前添加的 CSS
  • 找不到任何满足 newrelic-plugin-agent 要求的下载

    我尝试安装 MeetMenewrelic plugin agent如中所述https github com MeetMe newrelic plugin agent https github com MeetMe newrelic plug
  • DirectX12 - 执行命令列表和呈现函数

    我在微软的示例中发现 void D3D12HelloTriangle OnRender Record all the commands we need to render the scene into the command list Po
  • 如何检查进程是否具有管理权限

    如何正确检查进程是否以管理权限运行 我检查了IsUserAnAdimMSDN 中的函数 https msdn microsoft com en us library windows desktop bb776463 aspx 但不建议这样做
  • Angular 7:从订阅内部调用时,ChangeDetectorRef detectorChanges() 会导致无限循环

    在阅读了与变更检测和类似帖子相关的所有材料并未能解决我的问题后 我在这里发帖 变化检测器参考detectChanges 从订阅内部调用时会导致无限循环 如果我不打电话detectChanges I get ExpressionChanged
  • Python Pandas read_csv 跳过前 x 和后 y 行

    我想我可能在这里遗漏了一些明显的东西 但我对 python 和 pandas 很陌生 我正在读取一个大文本文件 只想使用范围 61 75496 中的行 我可以跳过前 60 行 keywords pd read csv keywords li
  • 如何在 Spring Boot Maven 多模块项目中包含另一个模块的资源

    我有一个 Spring Boot Maven 多模块项目 如果spring boot模块依赖模块A并在src main resources模块文件夹A我想在最终的 Spring Boot 应用程序中捆绑一个属性文件或其他一些资源 我该如何实
  • nil:NilClass 的未定义方法“map”

    当用户尝试更新其个人资料时 我的应用程序似乎随机抛出 nil NilClass 的未定义方法 map 错误 但奇怪的是 它说更新时发生错误 但错误行实际上在视图中 完整错误 users update ActionView TemplateE
  • Dynamic_cast 没有按预期抛出异常

    基于这个答案 在 C 中查找对象的类型 https stackoverflow com questions 351845 finding the type of an object in c 我写了这段代码 static TVALUE ge
  • 当我对符号矩阵进行行归约时,为什么 SymPy 给出了错误的答案?

    如果我要求 SymPy 对奇异矩阵进行行归约 nu Symbol nu lamb Symbol lambda A3 Matrix 3 nu 1 0 0 3 nu 2 nu 1 2 0 0 2 nu 1 nu lamb 2 3 0 0 nu
  • Safari 中的 Vue.js 渲染问题

    我有一个使用 Vue 和 Laravel 编写的作品集网站 它使用 v for 呈现项目缩略图 除了 Safari 之外 这在每个浏览器上都工作得很好 但有一个奇怪的问题 除非用户调整浏览器窗口的大小 否则图像根本不会显示 相关代码如下 d
  • 如何在使用 Python 运行 wkhtmltopdf.exe 时停止弹出窗口

    我正在使用 wkhtmltopdf exe 使用 python 将 html 转换为 pdf wkhtmltopdf exe 的弹出窗口使我在运行它时很难处理任何其他事情 我必须转换大约 200K 个文件 并且必须同时处理其他事情 但它确实
  • 是否可以将摘要式身份验证与 XMLHTTPRequest 一起使用?

    我有一个简单的问题 是否可以将摘要式身份验证与 XMLHTTPRequest 一起使用 如果答案是否定的 那么技术原因是什么 或者如果可能的话 我该怎么做 非常感谢 谷歌到目前为止还没有好的答案 EDIT 感谢您的回答 在收到随机数后修改标
  • 设置 WPF ScrollViewer 中何时滚动

    我有一个滚动查看器 其中包含一个网格 其中有一堆表单控件 文本框 复选框 组合框等 当我通过选项卡浏览控件时 滚动查看器将滚动 但仅在必要时滚动 我的意思是我通过选项卡浏览滚动查看器中的所有内容 并且仅当控件不可见时滚动查看器才会滚动 我想
  • 为什么这些线程不按顺序运行?

    当我运行这段代码时 include
  • 在 Laravel 中同步一对多关系

    如果我有一个多对多的关系 那么用它的关系更新关系是非常容易的sync method 但是我用什么来同步一对多关系呢 table posts id name table links id name post id 在这里 每一个Post可以有
  • Google OAuth:如何使用刷新令牌?

    我可以将 Android 设备上的一次性使用令牌换成访问令牌 and a 刷新令牌 我正在尝试弄清楚如何使用刷新令牌 I found this https developers google com accounts docs OAuth2
  • 在 R 中导入和分析非矩形 .csv 文件

    我将从 Mathematica 迁移到 R 在导入过程中我不需要预测数据结构 特别是我不需要在导入之前预测数据的矩形性 我有很多文件 csv文件格式如下 tasty chicken cinnamon not tasty butter pep