我有10个数据集,这些数据集是使用xlsx库从Excel文件中读取的,并存储在tibbles中.我想合并他们.
以下是示例数据集.数据集之间的变量数量不同,而某些变量仅在一个数据集中.person变量的值永远不会重叠.
data1 <- tibble(person = c("A","B","C"),
test1 = as.factor(c(1,4,5)),
test2 = c(14,25,10),
test3 = c(12.5,16.0,4),
test4 = c(16,23,21),
test5 = as.factor(c(49,36,52)))
data2 <- tibble(person = c("D","E","F"),
test1 = c(8,7,2),
test3 = c(6.5,12.0,19.5),
test4 = as.factor(c(15,21,29)),
test5 = as.factor(c(54,51,36)),
test6 = c(32,32,29),
test7 = c(13,11,10))
Run Code Online (Sandbox Code Playgroud)
实际数据集通常包含约50行和约200个变量.我试过了
all_data <- dplyr::bind_rows(data1,data2)
Run Code Online (Sandbox Code Playgroud)
希望得到这个结果
# A tibble: 6 x 8
person test1 test2 test3 test4 test5 test6 test7
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A 1 14 …
Run Code Online (Sandbox Code Playgroud) 我主要在fromat 的小标题中使用表格tidyverse
,但是对于某些步骤,我使用了data.table
包装。我想看到的是转换的最佳途径data.table回tibble?
我知道它data.table
具有一些巧妙的功能setDT和setDF功能,它们可以通过引用从data.frame转换为data.table(反之亦然),即无需复制。
但是,如果我想转换回小标题怎么办?我使用复制数据as_tibble
的data.frame从产生的setDT()
?是否有聪明的方法可以使用此方法,也许可以使用setattr()
from方法data.table
?
library(data.table)
library(tidyverse)
iris_tib <- as_tibble(iris)
## some data.table operation
setDT(iris_tib)
setkey(iris_tib, Species)
iris_tib[, Sepal.Length.Mean := mean(Sepal.Length), by = Species]
## How to convert back to tibble efficiently?
setDF(iris_tib)
iris_tib_back <- as_tibble(iris_tib)
## it looks like we were able to update by reference? Only rownames …
Run Code Online (Sandbox Code Playgroud) 我想枚举分组产生的数据帧/标题的每条记录。该索引是按照定义的顺序排列的。如果我使用 row_number() 它确实会枚举但在组内。但我希望它在不考虑前一组的情况下进行枚举。
这是一个例子。为了简单起见,我使用了最小的数据框:
library(dplyr)
df0 <- data.frame( x1 = rep(LETTERS[1:2],each=2)
, x2 = rep(letters[1:2], 2)
, y = floor(abs(rnorm(4)*10))
)
df0
# x1 x2 y
# 1 A a 12
# 2 A b 24
# 3 B a 0
# 4 B b 12
Run Code Online (Sandbox Code Playgroud)
现在,我将此表分组:
df1 <- df0 %>% group_by(x1,x2) %>% summarize(y=sum(y))
Run Code Online (Sandbox Code Playgroud)
这给了我一个 tibble 类的对象:
# A tibble: 4 x 3
# Groups: x1 [?]
# x1 x2 y
# <fct> <fct> <dbl>
# 1 A a 12 …
Run Code Online (Sandbox Code Playgroud) 我有一个包含2列的数据框:日期和返回值。
df <- tibble(
date = lubridate::today() +0:9,
return= c(1,2.5,2,3,5,6.5,1,9,3,2))
Run Code Online (Sandbox Code Playgroud)
现在,我想添加第三列,条件为ifelse。如果第t天的收益大于3.5,则第二天t + 1的重运行为NA(否则=第t天的收益)。
这是我想要的输出:
date return retrun_subsequent_day
<date> <dbl> <dbl>
1 2019-03-14 1 1
2 2019-03-15 2.5 2.5
3 2019-03-16 2 2
4 2019-03-17 3 3
5 2019-03-18 5 5
6 2019-03-19 6.5 NA
7 2019-03-20 1 NA
8 2019-03-21 9 9
9 2019-03-22 3 NA
10 2019-03-23 2 2
Run Code Online (Sandbox Code Playgroud)
有人可以描述我如何制定这种条件吗?
我想采用 gt() 表并将其转换为“宽”格式,而不是按组级别的“长”格式。因此,以使用 iris 数据集为例:
library(dplyr)
library(gt)
iris %>%
group_by(Species) %>%
slice_max(Sepal.Length, n=5) %>%
group_by(Species) %>%
gt()
Run Code Online (Sandbox Code Playgroud)
这会产生:
但是,我想要生成的是以下内容:
有没有办法做到这一点?
这是如何将行添加到仅修改某些列的数据帧的后续问题。
解决这个问题后,我想将 stefan 提供的解决方案应用到更大的数据框group_by
:
我的数据框:
df <- structure(list(test_id = c(1, 1, 1, 1, 1, 1, 1, 1), test_nr = c(1,
1, 1, 1, 2, 2, 2, 2), region = c("A", "B", "C", "D", "A", "B",
"C", "D"), test_value = c(3, 1, 1, 2, 4, 2, 4, 1)), class = "data.frame", row.names = c(NA,
-8L))
test_id test_nr region test_value
1 1 1 A 3
2 1 1 B 1
3 1 1 C 1
4 1 1 D …
Run Code Online (Sandbox Code Playgroud) 编辑 quarto/rmarkdown 文档时,我希望 RStudio 以与控制台中相同的方式显示内联标题,而不是分页的默认打印。
\n而不是这个:
\n\n我更喜欢控制台的输出:
\n# A tibble: 150 \xc3\x97 5\n Sepal.Length Sepal.Width Petal.Length Petal.Width Species\n <dbl> <dbl> <dbl> <dbl> <fct> \n 1 5.1 3.5 1.4 0.2 setosa \n 2 4.9 3 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 3.6 1.4 0.2 setosa \n 6 5.4 3.9 1.7 0.4 setosa \n 7 4.6 3.4 1.4 0.3 setosa \n 8 5 3.4 …
Run Code Online (Sandbox Code Playgroud) 我写了一个函数部分,将矩阵转换为小标题。这在 tibble 1.4.2 中没有问题,但在 2.0.1 中会导致错误。
导致错误的代码如下
library(tibble)
library(magrittr)
testmerge <- matrix( data = NA, ncol = 6 + 1, nrow = 0) %>%
as.tibble
Run Code Online (Sandbox Code Playgroud)
错误信息如下
我可以通过执行以下操作来解决问题
testmerge <- matrix( data = NA, ncol = 6 + 1, nrow = 0) %>%
as.data.frame() %>%
as_tibble
Run Code Online (Sandbox Code Playgroud)
但这似乎有点啰嗦。
是什么导致了这种变化?我怎么能轻易地得到一小撮空列呢?
我有一个嵌套的列表列表:
data = list(a = list(1, 2, 3), b = list("foo"), c = list("toast", "onions"))
Run Code Online (Sandbox Code Playgroud)
如何将其转换为 data.frame 或 tibble 的单行?我希望将具有多个元素的列表(a
此处c
)保留为列表,并将单元素(b
)作为常规值。
预期输出是:
# A tibble: 1 x 3
a b c
<list> <chr> <list>
1 <list [3]> foo <list [2]>
Run Code Online (Sandbox Code Playgroud) Tibbles 打印时使用行号作为行名称。请参阅1, 2
下面的左边距:
tibble::as_tibble(mtcars)
# A tibble: 32 x 11
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
Run Code Online (Sandbox Code Playgroud)
我可以在争论tibble:::print.tbl()
或其他情况下禁止打印这些数字吗?我知道我可以使用:row.names = FALSE
中的参数,但是我没有得到它作为小标题的所有其他不错的打印选项,它只是像常规 data.frame 一样打印。print.data.frame
print.data.frame(as_tibble(mtcars), row.names = FALSE)
我想保持输出与print.tbl()
上面的输出相同,但没有行号。