将作为列表的 data.frame 转换为完整但字符的 data.frame

Posted

技术标签:

【中文标题】将作为列表的 data.frame 转换为完整但字符的 data.frame【英文标题】:Convert data.frame that is a list to data.frame that is intact but characters 【发布时间】:2020-08-10 07:36:15 【问题描述】:

我有一个 data.frame,它由每个 URL 的行组成,但是当我运行“typeof”时它的格式是一个列表。当我尝试将其绑定到其他字符或数字格式的 data.frames 时,这会产生一些问题。我正在尝试将这个显示每行因子级别列表的 data.frame 转换为标准 data.frame,其中每个 URL 都完整地占据每一行(长度不变)。

我尝试了以下方法:

test 这给我留下了一个只是数字的向量,不再像我开始使用的任何东西

测试

这是其中一部分的输出:

structure(c(35L, 35L, 34L, 33L, 33L, 33L, 32L, 32L, 32L, 32L), .Label = c("/Archives/edgar/data/0001218981/000117911016022379/0001179110-16-022379-index.htm", 
"/Archives/edgar/data/0001218981/000117911016028590/0001179110-16-028590-index.htm", 
"/Archives/edgar/data/0001218981/000117911016028764/0001179110-16-028764-index.htm", 
"/Archives/edgar/data/0001218981/000117911016028975/0001179110-16-028975-index.htm", 
"/Archives/edgar/data/0001218981/000117911016029122/0001179110-16-029122-index.htm", 
"/Archives/edgar/data/0001218981/000117911016030044/0001179110-16-030044-index.htm", 
"/Archives/edgar/data/0001218981/000117911016031144/0001179110-16-031144-index.htm", 
"/Archives/edgar/data/0001218981/000117911016032519/0001179110-16-032519-index.htm", 
"/Archives/edgar/data/0001218981/000117911017003489/0001179110-17-003489-index.htm", 
"/Archives/edgar/data/0001218981/000117911017009339/0001179110-17-009339-index.htm", 
"/Archives/edgar/data/0001218981/000117911017009836/0001179110-17-009836-index.htm", 
"/Archives/edgar/data/0001218981/000117911017011451/0001179110-17-011451-index.htm", 
"/Archives/edgar/data/0001218981/000117911017011684/0001179110-17-011684-index.htm", 
"/Archives/edgar/data/0001218981/000117911017011782/0001179110-17-011782-index.htm", 
"/Archives/edgar/data/0001218981/000117911017013512/0001179110-17-013512-index.htm", 
"/Archives/edgar/data/0001218981/000117911017013668/0001179110-17-013668-index.htm", 
"/Archives/edgar/data/0001218981/000117911017013817/0001179110-17-013817-index.htm", 
"/Archives/edgar/data/0001218981/000117911017014024/0001179110-17-014024-index.htm", 
"/Archives/edgar/data/0001218981/000117911017014676/0001179110-17-014676-index.htm", 
"/Archives/edgar/data/0001218981/000117911017015379/0001179110-17-015379-index.htm", 
"/Archives/edgar/data/0001218981/000117911018004779/0001179110-18-004779-index.htm", 
"/Archives/edgar/data/0001218981/000117911018008826/0001179110-18-008826-index.htm", 
"/Archives/edgar/data/0001218981/000117911018009648/0001179110-18-009648-index.htm", 
"/Archives/edgar/data/0001218981/000117911018009739/0001179110-18-009739-index.htm", 
"/Archives/edgar/data/0001218981/000117911018011490/0001179110-18-011490-index.htm", 
"/Archives/edgar/data/0001218981/000117911019004050/0001179110-19-004050-index.htm", 
"/Archives/edgar/data/0001218981/000117911019004286/0001179110-19-004286-index.htm", 
"/Archives/edgar/data/0001218981/000117911019004323/0001179110-19-004323-index.htm", 
"/Archives/edgar/data/0001218981/000117911019005598/0001179110-19-005598-index.htm", 
"/Archives/edgar/data/0001218981/000117911019007384/0001179110-19-007384-index.htm", 
"/Archives/edgar/data/0001218981/000117911019012669/0001179110-19-012669-index.htm", 
"/Archives/edgar/data/0001218981/000117911019013159/0001179110-19-013159-index.htm", 
"/Archives/edgar/data/0001218981/000117911020000425/0001179110-20-000425-index.htm", 
"/Archives/edgar/data/0001218981/000117911020004224/0001179110-20-004224-index.htm", 
"/Archives/edgar/data/0001218981/000117911020004802/0001179110-20-004802-index.htm", 
"/Archives/edgar/data/0001218981/000120919115082536/0001209191-15-082536-index.htm", 
"/Archives/edgar/data/0001218981/000120919115083517/0001209191-15-083517-index.htm", 
"/Archives/edgar/data/0001218981/000120919116095084/0001209191-16-095084-index.htm", 
"/cgi-bin/browse-edgar?action=getcompany&CIK=0001015739", "/cgi-bin/browse-edgar?action=getcompany&CIK=0001218981&owner=include", 
"/cgi-bin/browse-edgar?action=getcompany&CIK=0001326732", "/cgi-bin/browse-edgar?action=getcurrent", 
"/cgi-bin/own-disp?action=getissuer&CIK=0001015739", "/cgi-bin/own-disp?action=getissuer&CIK=0001326732", 
"/cgi-bin/own-disp?action=getowner&CIK=0001218981&sortid=filings-ASC", 
"/cgi-bin/own-disp?action=getowner&CIK=0001218981&sortid=ownership-data-ASC", 
"/cgi-bin/own-disp?action=getowner&CIK=0001218981&sortid=period-of-report-ASC", 
"/cgi-bin/own-disp?action=getowner&CIK=0001218981&sortid=type-of-owner-ASC", 
"/edgar/searchedgar/webusers.htm", "/index.htm", "javascript:history.back()"
), class = "factor")

双重检查输入:

structure(list(web = structure(c(35L, 35L, 34L, 33L, 33L, 33L, 
32L, 32L, 32L, 32L, 31L, 31L, 31L, 30L, 29L, 27L, 28L, 27L, 26L, 
26L, 25L, 24L, 24L, 24L, 23L, 23L, 23L, 23L, 23L, 22L, 22L, 22L, 
21L, 20L, 20L, 19L, 19L, 18L, 18L, 18L, 17L, 17L, 17L, 16L, 16L, 
16L, 15L, 15L, 15L, 14L, 14L, 13L, 13L, 13L, 12L, 12L, 11L, 10L, 
9L, 8L, 7L, 7L, 6L, 6L, 6L, 5L, 4L, 4L, 3L, 2L, 2L, 2L, 1L, 38L, 
38L, 37L, 37L, 37L, 36L, 36L), .Label = c("/Archives/edgar/data/0001218981/000117911016022379/0001179110-16-022379-index.htm", 
"/Archives/edgar/data/0001218981/000117911016028590/0001179110-16-028590-index.htm", 
"/Archives/edgar/data/0001218981/000117911016028764/0001179110-16-028764-index.htm", 
"/Archives/edgar/data/0001218981/000117911016028975/0001179110-16-028975-index.htm", 
"/Archives/edgar/data/0001218981/000117911016029122/0001179110-16-029122-index.htm", 
"/Archives/edgar/data/0001218981/000117911016030044/0001179110-16-030044-index.htm", 
"/Archives/edgar/data/0001218981/000117911016031144/0001179110-16-031144-index.htm", 
"/Archives/edgar/data/0001218981/000117911016032519/0001179110-16-032519-index.htm", 
"/Archives/edgar/data/0001218981/000117911017003489/0001179110-17-003489-index.htm", 
"/Archives/edgar/data/0001218981/000117911017009339/0001179110-17-009339-index.htm", 
"/Archives/edgar/data/0001218981/000117911017009836/0001179110-17-009836-index.htm", 
"/Archives/edgar/data/0001218981/000117911017011451/0001179110-17-011451-index.htm", 
"/Archives/edgar/data/0001218981/000117911017011684/0001179110-17-011684-index.htm", 
"/Archives/edgar/data/0001218981/000117911017011782/0001179110-17-011782-index.htm", 
"/Archives/edgar/data/0001218981/000117911017013512/0001179110-17-013512-index.htm", 
"/Archives/edgar/data/0001218981/000117911017013668/0001179110-17-013668-index.htm", 
"/Archives/edgar/data/0001218981/000117911017013817/0001179110-17-013817-index.htm", 
"/Archives/edgar/data/0001218981/000117911017014024/0001179110-17-014024-index.htm", 
"/Archives/edgar/data/0001218981/000117911017014676/0001179110-17-014676-index.htm", 
"/Archives/edgar/data/0001218981/000117911017015379/0001179110-17-015379-index.htm", 
"/Archives/edgar/data/0001218981/000117911018004779/0001179110-18-004779-index.htm", 
"/Archives/edgar/data/0001218981/000117911018008826/0001179110-18-008826-index.htm", 
"/Archives/edgar/data/0001218981/000117911018009648/0001179110-18-009648-index.htm", 
"/Archives/edgar/data/0001218981/000117911018009739/0001179110-18-009739-index.htm", 
"/Archives/edgar/data/0001218981/000117911018011490/0001179110-18-011490-index.htm", 
"/Archives/edgar/data/0001218981/000117911019004050/0001179110-19-004050-index.htm", 
"/Archives/edgar/data/0001218981/000117911019004286/0001179110-19-004286-index.htm", 
"/Archives/edgar/data/0001218981/000117911019004323/0001179110-19-004323-index.htm", 
"/Archives/edgar/data/0001218981/000117911019005598/0001179110-19-005598-index.htm", 
"/Archives/edgar/data/0001218981/000117911019007384/0001179110-19-007384-index.htm", 
"/Archives/edgar/data/0001218981/000117911019012669/0001179110-19-012669-index.htm", 
"/Archives/edgar/data/0001218981/000117911019013159/0001179110-19-013159-index.htm", 
"/Archives/edgar/data/0001218981/000117911020000425/0001179110-20-000425-index.htm", 
"/Archives/edgar/data/0001218981/000117911020004224/0001179110-20-004224-index.htm", 
"/Archives/edgar/data/0001218981/000117911020004802/0001179110-20-004802-index.htm", 
"/Archives/edgar/data/0001218981/000120919115082536/0001209191-15-082536-index.htm", 
"/Archives/edgar/data/0001218981/000120919115083517/0001209191-15-083517-index.htm", 
"/Archives/edgar/data/0001218981/000120919116095084/0001209191-16-095084-index.htm", 
"/cgi-bin/browse-edgar?action=getcompany&CIK=0001015739", "/cgi-bin/browse-edgar?action=getcompany&CIK=0001218981&owner=include", 
"/cgi-bin/browse-edgar?action=getcompany&CIK=0001326732", "/cgi-bin/browse-edgar?action=getcurrent", 
"/cgi-bin/own-disp?action=getissuer&CIK=0001015739", "/cgi-bin/own-disp?action=getissuer&CIK=0001326732", 
"/cgi-bin/own-disp?action=getowner&CIK=0001218981&sortid=filings-ASC", 
"/cgi-bin/own-disp?action=getowner&CIK=0001218981&sortid=ownership-data-ASC", 
"/cgi-bin/own-disp?action=getowner&CIK=0001218981&sortid=period-of-report-ASC", 
"/cgi-bin/own-disp?action=getowner&CIK=0001218981&sortid=type-of-owner-ASC", 
"/edgar/searchedgar/webusers.htm", "/index.htm", "javascript:history.back()"
), class = "factor")), row.names = c(NA, -80L), class = "data.frame")

感谢您的帮助!

【问题讨论】:

str 表明它是一个 vectorfactor。不清楚你期望的输出是什么 我希望输出不再是一个因素,而是继续使用相同的文本 这是带有 as.character(test) 的输出:"c(35, 35, 34, 33, 33, 33, 32, 32, 32, 32, 31, 31, 31, 30 , 29, 27, 28, 27, 26, 26, 25, 24, 24, 24, 23, 23, 23, 23, 23, 22, 22, 22, 21, 20, 20, 19, 19, 18, 18 , 18, 17, 17, 17, 16, 16, 16, 15, 15, 15, 14, 14, 13, 13, 13, 12, 12, 11, 10, 9, 8, 7, 7, 6, 6 , 6, 5, 4, 4, 3, 2, 2, 2, 1, 38, 38, 37, 37, 37, 36, 36)" 我无法重现。我从你的输入中得到as.character(test)# [1] "/Archives/edgar/data/0001218981/000117911020004802/0001179110-20-004802-index.htm" [2] "/Archives/edgar/data/0001218981/000117911020004802/0001179110-20-004802-index.htm"... 【参考方案1】:

如果我们需要将列转换为character,只需提取列并使用as.character

test2$web <- as.character(test2$web)

-检查结构

str(test2)
#'data.frame':  80 obs. of  1 variable:
# $ web: chr  "/Archives/edgar/data/0001218981/000117911020004802/0001179110-20-004802-index.htm" "/Archives/edgar/data/0001218981/000117911020004802/0001179110-20-004802-index.htm" "/Archives/edgar/data/0001218981/000117911020004224/0001179110-20-004224-index.htm" "/Archives/edgar/data/0001218981/000117911020000425/0001179110-20-000425-index.htm" ...

【讨论】:

以上是关于将作为列表的 data.frame 转换为完整但字符的 data.frame的主要内容,如果未能解决你的问题,请参考以下文章

如何将list转化为data frame

将数据框转换为列表的树结构列表

data.frame 方法的最有效列表?

将 R data.frame 强制转换为 nz.data.frame 时出错

如何将 data.frame 表转换为 xts 对象

如何将data.frame列从因子转换为数字[重复]