当我尝试回答关于R的Stack Overflow中的一个问题时,我花费了很多时间来尝试重建作为示例给出的数据(除非问题作者已经足够好以提供它们作为R代码).
所以我的问题是,如果有人问一个问题并按以下方式给出他的样本数据框:
a b c
1 11 foo
2 12 bar
3 13 baz
4 14 bar
5 15 foo
Run Code Online (Sandbox Code Playgroud)
您是否有一个提示或功能可以轻松地将其导入R会话,而无需键入整个data.frame()指令?
提前感谢任何暗示!
PS:对不起如果术语"查询"在我的问题标题中不是很好,但似乎你不能在堆栈溢出的问题标题中使用"问题"这个词:-)
Dir*_*tel 24
也许textConnection()这就是你想要的:
R> zz <- read.table(textConnection("a b c
1 11 foo
2 12 bar
3 13 baz
4 14 bar
5 15 foo"), header=TRUE)
R> zz
a b c
1 1 11 foo
2 2 12 bar
3 3 13 baz
4 4 14 bar
5 5 15 foo
R>
Run Code Online (Sandbox Code Playgroud)
它允许您将文本视为要从中读取的"连接".您也可以只复制和粘贴,但从剪贴板访问更依赖于操作系统,因此便携性较差.
42-*_*42- 22
最近版本的R现在提供了比将textConnection列数据输入read.table和朋友的路线更低的击键选项.面对这个:
zz
a b c
1 1 11 foo
2 2 12 bar
3 3 13 baz
4 4 14 bar
5 5 15 foo
Run Code Online (Sandbox Code Playgroud)
可以简单地插入:<- read.table(text="之后zz,删除回车符然后", header=TRUE)在最后一个之后插入foo并输入[enter].
zz<- read.table(text=" a b c
1 1 11 foo
2 2 12 bar
3 3 13 baz
4 4 14 bar
5 5 15 foo", header=TRUE)
Run Code Online (Sandbox Code Playgroud)
人们还可以scan用来有效地输入纯数字或纯字符向量条目的长序列.面对:67 75 44 25 99 37 6 96 77 21 31 41 5 52 13 46 14 70 100 18,可以简单地输入:zz <- scan()并按[enter].然后粘贴所选的数字并再次点击[输入],也许第二次再次进行双回车,控制台应该响应"读取20项".
> zz <- scan()
1: 67 75 44 25 99 37 6 96 77 21 31 41 5 52 13 46 14 70 100 18
21:
Read 20 items
Run Code Online (Sandbox Code Playgroud)
"角色"任务.粘贴到控制台并编辑无关的换行符并添加引号后,然后按[enter]:
> countries <- scan(what="character")
1: 'republic of congo'
2: 'republic of the congo'
3: 'congo, republic of the'
4: 'congo, republic'
5: 'democratic republic of the congo'
6: 'congo, democratic republic of the'
7: 'dem rep of the congo'
8:
Read 7 items
Run Code Online (Sandbox Code Playgroud)
huo*_*uon 13
您也可以要求提问者使用以dput可以将其复制粘贴到R中的方式转储任何数据结构的函数.例如
> zz
a b c
1 1 11 foo
2 2 12 bar
3 3 13 baz
4 4 14 bar
5 5 15 foo
> dput(zz)
structure(list(a = 1:5, b = 11:15, c = structure(c(3L, 1L, 2L,
1L, 3L), .Label = c("bar", "baz", "foo"), class = "factor")), .Names = c("a",
"b", "c"), class = "data.frame", row.names = c(NA, -5L))
> xx <- structure(list(a = 1:5, b = 11:15, c = structure(c(3L, 1L, 2L,
+ 1L, 3L), .Label = c("bar", "baz", "foo"), class = "factor")), .Names = c("a",
+ "b", "c"), class = "data.frame", row.names = c(NA, -5L))
> xx
a b c
1 1 11 foo
2 2 12 bar
3 3 13 baz
4 4 14 bar
5 5 15 foo
Run Code Online (Sandbox Code Playgroud)
只是想添加这个,因为我现在经常使用它,并且我认为它非常有用。有一个包溢出(下面的安装说明)具有读取复制数据帧的功能。假设我从一篇 SO 帖子开始,其中包含如下所示的数据,但没有dput输出。
Sepal.Length Sepal.Width Petal.Length Petal.Width Species\n1 5.1 3.5 1.4 0.2 setosa\n2 4.9 3.0 1.4 0.2 setosa\n3 4.7 3.2 1.3 0.2 setosa\n4 4.6 3.1 1.5 0.2 setosa\n5 5.0 3.6 1.4 0.2 setosa\n6 5.4 3.9 1.7 0.4 setosa\nRun Code Online (Sandbox Code Playgroud)\n\n现在,如果我直接复制该数据,然后运行以下命令
\n\nlibrary(overflow)\nsoread()\n# data.frame \xe2\x80\x9cmydf\xe2\x80\x9d created in your workspace\n# Sepal.Length Sepal.Width Petal.Length Petal.Width Species\n# 1 5.1 3.5 1.4 0.2 setosa\n# 2 4.9 3.0 1.4 0.2 setosa\n# 3 4.7 3.2 1.3 0.2 setosa\n# 4 4.6 3.1 1.5 0.2 setosa\n# 5 5.0 3.6 1.4 0.2 setosa\n# 6 5.4 3.9 1.7 0.4 setosa\nRun Code Online (Sandbox Code Playgroud)\n\n我现在有一个数据框,其名称mydf与我在全局环境中复制的数据框相同,因此我不必等待 OP 发布dput其数据框。我可以使用参数更改数据框的名称out,(显然)默认为mydf. 还有一些其他有用的功能可用于处理包中的 SO 帖子(例如sopkgs(),它会临时安装一个包,以便您可以帮助解决有关您之前未安装的包的问题)。
如果您留library(overflow)在您的 中.Rprofile,那么soread()可以非常快速地从 SO 帖子导入数据。
Overflow可从 GitHub 获取,并且可以使用以下命令安装
\n\nlibrary(devtools)\ninstall_github("overflow", "sebastian-c")\nRun Code Online (Sandbox Code Playgroud)\n