![](/img/trans.png)
[英]import comma delimited text file with key value pairs into R as a dataframe
[英]How to convert key value pairs i a text file into a dataframe in R
我有這樣的數據:
3 : 124wr332::::::::::::::::Ident:::123456A::::::::::::::::img src="Images Captured with Proc Data Audit.files/img21.jpg" width="152" height="99" alt="":::::::::Date of procedure:::2018-03-01::::::::Procedure Performed:::A thingy::::::::Image label:::Top of:::::::::::::::::::Image Comment:::: ::::::::::::::::::::::::::::::Ident:::123456A::::::::::::::::img src="Images Captured with Proc Data Audit.files/img22.jpg" width="152" height="99" alt="":::::::::Date of procedure:::2018-03-01::::::::Procedure Performed:::A thingy::::::::Image label:::Gastric Antrum:::::::::::::::::::Image Comment:::: ::::::::::::::::::::::::::::::Patient MRN:::123456A::::::::::::::::img src="Images Captured with Proc Data Audit.files/img23.jpg" width="152" height="99" alt="":::::::::Date of procedure:::2018-03-01::::::::Procedure Performed:::Gastroscopy (OGD)::::::::Image label:::Other place:::::::::::::::::::Image Comment:::: ::::::::::::::::::::::::::::::::::::::::::07/05/2018 13:25:45::::::::::Page 2::::::::::::::::::::::::::::::::Ident:::123456A::::::::::::::::img src="Images Captured with Proc Data Audit.files/img16.jpg" width="152" height="99" alt="":::::::::Date of procedure:::2018-03-01::::::::Procedure Performed:::A thingy::::::::Image label:::2nd part:::::::::::::::::::Image Comment:::Lovely: ::::::::::::::::::::::::::::::
但我希望它位於 dataframe 中,其中列 label 是任何關鍵定義為 label 出現在三個之前:如下:
Ident img DateOfProcedure ProcedurePerformed ImageLabel ImageComment
123456A 21 2018-03-01 A thingy Top of
123456A 22 2018-03-01 A thingy Other place
123456A 23 2018-03-01 A thingy 2nd part Lovely
我嘗試使用a <- strsplit(as.character(my$key_value), ":::")
但它給了我不同行上的鍵值對,而我希望鍵是上面的列名
splitstackshape
package 是您所需要的。 請參見下面的示例:
library(splitstackshape)
library(tidyverse)
x <- '3 : 124wr332::::::::::::::::Ident:::123456A::::::::::::::::img src="Images Captured with Proc Data Audit.files/img21.jpg" width="152" height="99" alt="":::::::::Date of procedure:::2018-03-01::::::::Procedure Performed:::A thingy::::::::Image label:::Top of:::::::::::::::::::Image Comment:::: ::::::::::::::::::::::::::::::Ident:::123456A::::::::::::::::img src="Images Captured with Proc Data Audit.files/img22.jpg" width="152" height="99" alt="":::::::::Date of procedure:::2018-03-01::::::::Procedure Performed:::A thingy::::::::Image label:::Gastric Antrum:::::::::::::::::::Image Comment:::: ::::::::::::::::::::::::::::::Patient MRN:::123456A::::::::::::::::img src="Images Captured with Proc Data Audit.files/img23.jpg" width="152" height="99" alt="":::::::::Date of procedure:::2018-03-01::::::::Procedure Performed:::Gastroscopy (OGD)::::::::Image label:::Other place:::::::::::::::::::Image Comment:::: ::::::::::::::::::::::::::::::::::::::::::07/05/2018 13:25:45::::::::::Page 2::::::::::::::::::::::::::::::::Ident:::123456A::::::::::::::::img src="Images Captured with Proc Data Audit.files/img16.jpg" width="152" height="99" alt="":::::::::Date of procedure:::2018-03-01::::::::Procedure Performed:::A thingy::::::::Image label:::2nd part:::::::::::::::::::Image Comment:::Lovely: ::::::::::::::::::::::::::::::'
data.frame(col = x) %>%
splitstackshape::cSplit(splitCols = "col", sep = ":::") %>%
glimpse
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.