我的数据框包含具有选择多个问题类型的调查的输出.一些单元格具有多个值.
df <- data.frame(a=1:3,b=I(list(1,1:2,1:3)))
df
a b
1 1 1
2 2 1, 2
3 3 1, 2, 3
Run Code Online (Sandbox Code Playgroud)
我想将列表弄平以获得以下输出:
df
a b
1 1 1
2 2 1
3 2 2
4 3 1
5 3 2
6 3 3
Run Code Online (Sandbox Code Playgroud)
应该很容易但不知何故我找不到搜索词.谢谢.
受@ gsk3关于重塑数据的问题的评论的启发,我开始对重组数据进行一些实验,其中变量名称具有字符后缀而不是数字后缀.
例如,我dadmomw将从其中一个UCLA ATS Stata学习网页加载数据集(参见网页上的"示例4").
这是数据集的样子:
library(foreign)
dadmom <- read.dta("https://stats.idre.ucla.edu/stat/stata/modules/dadmomw.dat")
dadmom
# famid named incd namem incm
# 1 1 Bill 30000 Bess 15000
# 2 2 Art 22000 Amy 18000
# 3 3 Paul 25000 Pat 50000
Run Code Online (Sandbox Code Playgroud)
当试图从这种宽格式重塑到很长时间时,我遇到了一个问题.这是我重塑数据的方法.
reshape(dadmom, direction="long", idvar=1, varying=2:5,
sep="", v.names=c("name", "inc"), timevar="dadmom",
times=c("d", "m"))
# famid dadmom name inc
# 1.d 1 d 30000 Bill
# 2.d 2 d 22000 Art
# 3.d 3 d 25000 Paul …Run Code Online (Sandbox Code Playgroud) 如果我错过了这么简单的问题的答案,请原谅我.
我想用来cbind()绑定两列.其中一个是长度较短的单个条目.
我可以NA为缺失值提供R 吗?
文档讨论了一个deparse.level论点,但这似乎不是我的解决方案.
此外,如果我可能如此大胆,是否还有一种快速的方法来将较短的列添加到NA's?
我甚至不确定如何正确地标题!
假设我有一个数据帧d:
当前数据帧:
d <- data.frame(sample = LETTERS[1:2], cat = letters[11:20], count = c(1:10))
sample cat count
1 A k 1
2 B l 2
3 A m 3
4 B n 4
5 A o 5
6 B p 6
7 A q 7
8 B r 8
9 A s 9
10 B t 10
Run Code Online (Sandbox Code Playgroud)
我正在尝试重新安排一些事情,使每个cat值成为它自己的列,样本仍然是一列(或成为行名),count将是新cat列中的值,其中0表示样品没有猫的数量.像这样:
期望的数据帧布局:
sample k l m n o p q r s t
1 A 1 0 3 0 5 0 7 0 …Run Code Online (Sandbox Code Playgroud) 许多R用户最终想出了很多方法来从他们的数据中删除元素.一种方法是使用NULL,特别是当你想要做一些事情,比如从a中删除一个列data.frame或从一个元素中删除一个元素list.
最终,用户遇到他们想要立即从一个列中删除多个列的data.frame情况,并且他们将其<- list(NULL)作为解决方案(因为使用<- NULL将导致错误).
A data.frame 是一种特殊的类型list,因此想象从a中删除项目的方法list应该与从a中删除列的方法相同并不太难data.frame.但是,它们会产生不同的结果,如下例所示.
## Make some small data--two data.frames and two lists
cars1 <- cars2 <- head(mtcars)[1:4]
cars3 <- cars4 <- as.list(cars2)
## Demonstration that the `list(NULL)` approach works
cars1[c("mpg", "cyl")] <- list(NULL)
cars1
# disp hp
# Mazda RX4 160 110
# Mazda RX4 Wag 160 110
# Datsun 710 108 93
# Hornet 4 Drive …Run Code Online (Sandbox Code Playgroud) 我试图实现类似于这个问题的东西,但是必须用NA替换多个值,并且在大型数据集中.
df <- data.frame(name = rep(letters[1:3], each = 3), foo=rep(1:9),var1 = rep(1:9), var2 = rep(3:5, each = 3))
Run Code Online (Sandbox Code Playgroud)
生成此数据帧:
df
name foo var1 var2
1 a 1 1 3
2 a 2 2 3
3 a 3 3 3
4 b 4 4 4
5 b 5 5 4
6 b 6 6 4
7 c 7 7 5
8 c 8 8 5
9 c 9 9 5
Run Code Online (Sandbox Code Playgroud)
我想用NA替换所有出现的,例如3和4,但仅在以"var"开头的列中.
我知道我可以使用[]运算符的组合来实现我想要的结果:
df[,grep("^var[:alnum:]?",colnames(df))][
df[,grep("^var[:alnum:]?",colnames(df))] == 3 | …Run Code Online (Sandbox Code Playgroud) 假设(为了简化)我有一个包含一些控制与治疗数据的表:
Which, Color, Response, Count
Control, Red, 2, 10
Control, Blue, 3, 20
Treatment, Red, 1, 14
Treatment, Blue, 4, 21
Run Code Online (Sandbox Code Playgroud)
对于每种颜色,我想要一个包含控制和治疗数据的行,即:
Color, Response.Control, Count.Control, Response.Treatment, Count.Treatment
Red, 2, 10, 1, 14
Blue, 3, 20, 4, 21
Run Code Online (Sandbox Code Playgroud)
我想这样做的一种方法是在每个控件/处理子集上使用内部合并(在Color列上合并),但是有更好的方法吗?我在想重塑包或堆栈功能可以某种方式做到,但我不确定.
我正在尝试创建一个新闻文章数据框的子集,其中至少提到一组关键字或短语的一个元素.
# Sample data frame of articles
articles <- data.frame(id=c(1, 2, 3, 4), text=c("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod", "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,", "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo", "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"))
articles$text <- as.character(articles$text)
# Sample vector of keywords or phrases
keywords <- as.character(c("elit", "tempor incididunt", "reprehenderit"))
# id text
# 1 …Run Code Online (Sandbox Code Playgroud) 我试图复制一个数据帧(动物园对象)50次作为一个整体,并将结果作为一个矩阵,但我尝试过的所有命令似乎都不成功.我可以轻松编写一个可以执行此操作的函数,但我希望结果可以轻松实现rep.
以下面的例子为例
x <- zoo(data.frame(A = c(1,2,3,4,5,6), B = c(7,8,9,10,11,12), C = c(13,14,15,16,17,18)), order.by = seq(as.Date("2012-01-01"), as.Date("2012-06-01"), by = "month"))
#> x
# A B C
#2012-01-01 1 7 13
#2012-02-01 2 8 14
#2012-03-01 3 9 15
#2012-04-01 4 10 16
#2012-05-01 5 11 17
#2012-06-01 6 12 18
Run Code Online (Sandbox Code Playgroud)
我们试着复制x2次.我要找的最终结果是:
# [,1] [,2] [,3]
# [1,] 1 7 13
# [2,] 2 8 14
# [3,] 3 9 15
# [4,] 4 10 16
# …Run Code Online (Sandbox Code Playgroud) 在代码本身计算这些值之前,是否有标准方法在编写的knitr报告中尽早包含变量的计算值?目的是在报告的顶部创建一个执行摘要.
例如,像这样的东西,其中variable1和variable2直到后来才定义:
---
title: "Untitled"
output: html_document
---
# Summary
The values from the analysis are `r variable1` and `r variable2`
## Section 1
In this section we compute some values. We find that the value of variable 1 is `r variable1`
```{r first code block}
variable1 <- cars[4, 2]
```
## Section 2
In this section we compute some more values. In this section we compute some values. We find that the value of variable 2 is `r variable2` …Run Code Online (Sandbox Code Playgroud)