首先 - 我是编程和R的初学者,所以请原谅我这是一个愚蠢的问题.我在查看由以下代码生成的tibble中的十多行时遇到问题.
下面的代码旨在找到书中最常见的单词.我得到了我想要的结果,但是如何查看超过10行的数据.据我所知,它不会被保存为我可以调用的数据框架.
library(dplyr)
tidy_books %>%
anti_join(stop_words) %>%
count(word, sort=TRUE)
Joining, by = "word"
# A tibble: 3,397 x 2
word n
<chr> <int>
1 alice 820
2 queen 247
3 time 141
4 king 122
5 head 112
6 looked 100
7 white 97
8 round 96
9 voice 86
10 tone 81
# ... with 3,387 more rows
Run Code Online (Sandbox Code Playgroud) 我有一个带有3个cols的data.frame:日期,费率,价格.我想在价格和价格之前添加来自矩阵的列.
df = tibble('date' = c('01/01/2000', '02/01/2000', '03/01/2000'),
'rate' = c(7.50, 6.50, 5.54),
'price' = c(92, 94, 96))
Run Code Online (Sandbox Code Playgroud)
我使用输出矩阵的函数计算了速率的滞后:
rate_Lags = matrix(data = c(NA, 7.50, 5.54, NA, NA, 7.50), ncol=2, dimnames=list(c(), c('rate_tMinus1', 'rate_tMinus2'))
我想在使用名称索引而不是列顺序之后在费率(和价格之前)之后插入这些滞后.
add_column
来自tibble包的函数(在data.frame中的两列之间添加一列)不起作用,因为它只接受一个原子向量(因此如果我有10个滞后,我将不得不调用add_column 10次).我可以apply
在我的rate_Lags
矩阵中使用.然而,然而,我失去了rate_Lags
矩阵中的dimnames .
如果我知道特定列名的位置(任何检索位置的函数),使用数字索引(子集)(https://stat.ethz.ch/pipermail/r-help/2011-August/285534.html)就可以工作列名?).
有插入一串柱中的任何简单的方法小号在数据帧/ tibble对象的特定位置?
当我尝试在tibble中的一列字符上使用滞后函数(来自dplyr库)时,我在R中收到以下错误:
mutate_impl(.data,dots)出错:期望单个字符串值:[type = logical; 程度= 1].
数据框中的一列字符不会发生此错误.我也没有在tibble或数据框中得到一列数字的错误.
有谁知道为什么我在数据帧与反复的滞后函数中出现这种差异?谢谢!
以下是一些重现错误的示例代码.我有滞后工作和不工作时的例子.我已经尝试在我的机器上更新tidyverse和dplyr库,但我仍然遇到同样的错误.
tib = data_frame(x = c('a','b','c'), y = 1:3)
# lagging column of characters in tibble throws error
res = tib %>%
mutate(lag_n = lag(x, n=1, default = NA))
# lagging column of numbers in tibble does NOT throw error
res = tib %>%
mutate(lag_c = lag(y, n=1, default = NA))
df = data.frame(x = c('a','b','c'), y = 1:3)
# lagging column of characters in data frame does NOT throw error
res …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将“加载”类型的对象转换为 R 中的数据帧。但是,我尝试通过 as_tibble() 或 as.data.frame() 对其进行强制没有奏效。这是代码:
iris_pca <- prcomp(iris[1:4], center = TRUE, scale. = TRUE)
iris_pca$rotation[,1:2] %>%
varimax() %>%
.$loadings
Run Code Online (Sandbox Code Playgroud)
这打印出来:
Loadings:
PC1 PC2
Sepal.Length 0.596 -0.243
Sepal.Width -0.961
Petal.Length 0.570 0.114
Petal.Width 0.565
PC1 PC2
SS loadings 1.00 1.00
Proportion Var 0.25 0.25
Cumulative Var 0.25 0.50
Run Code Online (Sandbox Code Playgroud)
如何将这些数据放入数据框中?
我不知道创建一个dplyr::mutate()
带有单元素列表的新列表列实际上是深度复制元素以填充tibble长度(请参阅参考资料t3
).这是为什么?
如果我t4
在创建tibble(t5
)时指定了explicit()的正确长度或传递它,则元素将通过引用传递.
考虑以下情况,其中列表包含具有大向量的tibble.
library(tidyverse)
library(pryr)
t1 <- tibble(a = 1:4)
t2 <- tibble(b = 1:1e6)
t3 <- mutate(t1, tl = list(t2))
t4 <- mutate(t1, tl = rep(list(t2), n()))
t5 <- tibble(a = 1:4, tl = list(t2))
object_size(t2)
#> 4 MB
object_size(t3)
#> 16 MB
object_size(t4)
#> 4 MB
object_size(t5)
#> 4 MB
Run Code Online (Sandbox Code Playgroud)
由reprex包创建于2019-02-22 (v0.2.1)
我正在寻找一种方法,在 id 和组中,使用 100 的滞后(或领先) value
和新索引号idx_value
来计算下一个索引号。
# install.packages(c("tidyverse"), dependencies = TRUE)
library(tibble)
library(magrittr)
Run Code Online (Sandbox Code Playgroud)
就像,我有这个数据框:
start_tbl <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L), grp = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 2L, 2L), year = c(7L, 8L, 9L, 10L, 7L, 8L, 9L, 10L,
7L, 8L, 9L, 7L, 8L, 9L), value = c(2, -7, -2.3, 1.1, -1, -12,
-4, 2, 1, -3, 2, …
Run Code Online (Sandbox Code Playgroud) 我试图了解如何简洁地实现诸如参数捕获/解析/评估机制之类的东西,该机制可以使用dplyr::tibble()
(FKA dplyr::data_frame()
) 实现以下行为:
# `b` finds `a` in previous arg
dplyr::tibble(a=1:5, b=a+1)
## a b
## 1 2
## 2 3
## ...
# `b` can't find `a` bc it doesn't exist yet
dplyr::tibble(b=a+1, a=1:5)
## Error in eval_tidy(xs[[i]], unique_output) : object 'a' not found
Run Code Online (Sandbox Code Playgroud)
对于像和 这样base::
的类,这是不可能的(也许 bc 参数没有按顺序解释(?)和/或也许 bc 它们在父环境中进行评估(?)):data.frame
list
data.frame(a=1:5, b=a+1)
## Error in data.frame(a = 1:5, b = a + 1) : object 'a' not found
list(a=1:5, b=a+1)
## Error: …
Run Code Online (Sandbox Code Playgroud) 我想在表格中列出我的包中的所有功能。
到目前为止,我从包帮助文档中提取了所有功能和标题
library(magrittr)
package_info <- library(help = magrittr)$info[[2]]
package_info_tbl <- package_info %>%
stringr::str_split(pattern = "\\s+", n = 2, simplify = T) %>%
tibble::as_tibble(.name_repair = "minimal")
colnames(package_info_tbl) <- c("Function", "Title")
package_info_tbl
#> # A tibble: 13 x 2
#> Function Title
#> <chr> <chr>
#> 1 "%$%" magrittr exposition pipe-operator
#> 2 "%<>%" magrittr compound assignment pipe-operator
#> 3 "%>%" magrittr forward-pipe operator
#> 4 "%T>%" magrittr tee operator
#> 5 "[[.fseq" Extract function(s) from a functional sequence.
#> 6 "debug_fseq" …
Run Code Online (Sandbox Code Playgroud) 我一直在努力解决这个问题,但我根本找不到任何解决问题的方法。希望这里有人可以提供帮助。
我正在尝试为具有以下结构的某些数据创建个人选择矩阵:
# A tibble: 2,152 x 32
age choice canton lr_s dist_svp dist_fdp dist_bdp dist_cvp dist_glp dist_sp
<dbl> <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 39 sp GE 3 49 25 25 4 16 1
2 67 sp ZH 0 100 49 64 4 25 0
3 42 svp ZH 7 4 4 1 36 4 36
dist_gps pid_svp pid_fdp pid_bdp pid_cvp pid_glp pid_sp pid_gps french italian
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> …
Run Code Online (Sandbox Code Playgroud) 我有一个返回小标题的函数。它运行正常,但我想对其进行矢量化。
library(tidyverse)
tibTest <- tibble(argX = 1:4, argY = 7:4)
square_it <- function(xx, yy) {
if(xx >= 4){
tibble(x = NA, y = NA)
} else if(xx == 3){
tibble(x = as.integer(), y = as.integer())
} else if (xx == 2){
tibble(x = xx^2 - 1, y = yy^2 -1)
} else {
tibble(x = xx^2, y = yy^2)
}
}
Run Code Online (Sandbox Code Playgroud)
mutate
当我用 调用它时map2
,它运行正常,给了我想要的结果:
tibTest %>%
mutate(sq = map2(argX, argY, square_it)) %>%
unnest()
## A tibble: 3 x …
Run Code Online (Sandbox Code Playgroud)