我是 R 的新手。教授要求我们获取 Excel 格式的几列数据的总和、均值和方差。现在,我想尝试使用 R 来解决它们,而不是在 Excel 中输入公式并拖动。我已经将数据导入到 R 中并且它们被正确显示。我可以将命令sum ()和sd ()和var ()用于每个列。
我的问题是:有没有办法让 R 同时显示每列的总和、标准差和方差?(而不是为每一列一次又一次地计算这些)。
我的意思是类似colSum(col1, col2, col3,...)的东西,该行只显示每列的总和。
我有一个包含 2 列的数据框:
.id vals
1 A 10
2 B 20
3 C 30
4 A 100
5 B 200
6 C 300
dput(tst_df)
structure(list(.id = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("A",
"B", "C"), class = "factor"), vals = c(10, 20, 30, 100, 200,
300)), .Names = c(".id", "vals"), row.names = c(NA, -6L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
现在我想让该.id列成为我的列名称,并且 vals 将成为 2 行。
像这样:
A B C
10 20 30
100 200 300
Run Code Online (Sandbox Code Playgroud)
基本上 .id 是我的分组变量,我希望将属于 1 …
我已经看到了这个,并且我已经添加了这个包 MatrixStats。
但是当我运行代码时
colMeans(adult_csv[5], na.rm = TRUE)
colMedians(adult_csv[5], na.rm = TRUE)
Run Code Online (Sandbox Code Playgroud)
它通过了第一行,但抱怨colMedians:
R: 找不到函数 colMedians
假设我有一个从1到5的向量,
a<-c(1:5)
Run Code Online (Sandbox Code Playgroud)
我需要做的是通过连续丢失一个元素来重复向量.也就是说,最终结果应该是这样的
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Run Code Online (Sandbox Code Playgroud) 我正在尝试安装包,circular我在安装过程中遇到以下错误.
我尝试了几个帖子中提出的一些选项,但它没有用.我也尝试加载其他包,例如spatstat,这样做时不会发生此错误.
有谁可以帮我解决这个问题.
我想基于手动指定的字符向量订购数据表。
library(data.table)
DT = data.table(x=c("c","b","a"), y=1:3)
Run Code Online (Sandbox Code Playgroud)
我可以按字母顺序排序:
DT[order(x)]
Run Code Online (Sandbox Code Playgroud)
但我可以根据像这样的字符向量来订购它吗:
preferred.order <- c("b","a","c")
Run Code Online (Sandbox Code Playgroud)
目标是:
data.table(x=c("b","a","c"), y=c(2,1,3))
Run Code Online (Sandbox Code Playgroud)
实际上,我在第一列中有一个data.table,其中包含收集的输出和变量名称。为了便于演示,我希望这些变量以特定的顺序(而不是字母顺序)排列。
我希望每个col和group出现的所有相等/相同值(唯一== 1)设置为NA:
如果每组和col我们至少有2个不同的值,我想保留它们全部.
难以解释.以下是一些示例数据:
代表.例:
ds <- data.frame()
for (i in 1:3) {
for(ii in 1:3) {
ds <- rbind(mtcars[i,1:4],ds)
}
}
rownames(ds) <- NULL
ds[1,1] <- 1337;ds[2:3,3] <- 1337;ds[5,2] <- 1337;ds[8,1] <- 1337;
ds <- cbind(group=rep(1:3,each=3),ds,stringis=c("a","a","a","b","c","d","e","e","f"))
Run Code Online (Sandbox Code Playgroud)
看起来像:
> ds
group mpg cyl disp hp stringis
1 1 1337.0 4 108 93 a
2 1 22.8 4 1337 93 a
3 1 22.8 4 1337 93 a
4 2 21.0 6 160 110 b
5 2 21.0 1337 160 110 …Run Code Online (Sandbox Code Playgroud) 我有矢量x <- c(1:50),我想插入y <- c(1,2)每10个位置5次.
x <- c(1:50)
y <- c(1,2)
z <- c(seq(10,50,10))
Run Code Online (Sandbox Code Playgroud)
该产品必须是这样的:
1 2 3 4 5 6 7 8 9 10 1 2 11 12 13 14 15 16 17 18 19 20 1 2 21 22 23 24 25 26 27 28 29 30 1 2 31 32 33 34 35 36 37 38 39 40 1 2 41 42 43 44 45 46 47 48 49 50 1 2
Run Code Online (Sandbox Code Playgroud)
我尝试过追加,但它不使用"时代"......
我知道如果我有一个矩阵列表,我可以使用该Reduce函数在所有矩阵中应用一个操作.例如:
l <- list(matrix(rnorm(16), 4, 4), matrix(rnorm(16), 4, 4))
Reduce(`*`, l)
Run Code Online (Sandbox Code Playgroud)
但是如果我想在多个列表中应用此操作呢?我可以用for循环做一个暴力的方法,但我觉得应该有更好的方法.我可以用两个列表mapply
l2 <- l
mapply(`*`, l, l2, SIMPLIFY = FALSE)
Run Code Online (Sandbox Code Playgroud)
但如果我有更多这两个,我不知道如何解决这个问题.
以下想法都会导致错误:
l3 <- l2
mapply(`*`, l, l2, l3, SIMPLIFY = FALSE)
Error in .Primitive("*")(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
operator needs one or two arguments
Reduce(`*`, list(l, l2, l3))
Error in f(init, x[[i]]) : non-numeric argument to binary operator
Run Code Online (Sandbox Code Playgroud)
期望的输出是长度为2的列表,其中每个列表中的每个矩阵的元素乘积.蛮力循环看起来像这样:
out <- vector("list", length = 2)
for(i in 1:2){
out[[i]] <- l[[i]] * l2[[i]] * …Run Code Online (Sandbox Code Playgroud) 如何动态地将数据框中的所有列加载到列表中?
目前我就是这样做的,但这非常繁琐:
list(PM = PM, site = site, code = code)
Run Code Online (Sandbox Code Playgroud)
例如,我有这个数据名称:
structure(list(Particles = c(0.596667, 0.27, 0.153333, 0, 0.753333,
0, 0.35, 0.506667, 1.6, 0.116667), PM = c(35.5158928571429, 16.0714285714286,
9.12696428571429, 0, 44.84125, 0, 20.8333333333333, 30.15875,
95.2380952380953, 6.94446428571429), timestamp = c(1493310389147,
1493310419191, 1493310449254, 1493310479270, 1493310509313, 1493310539387,
1493310569416, 1493310599465, 1493310629525, 1502378711339),
date = structure(c(1493310389.147, 1493310419.191, 1493310449.254,
1493310479.27, 1493310509.313, 1493310539.387, 1493310569.416,
1493310599.465, 1493310629.525, 1502378711.339), class = c("POSIXct",
"POSIXt"), tzone = "UTC-1"), site = c("ABC", "ABC",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "ABC"
), …Run Code Online (Sandbox Code Playgroud) r ×10
dataframe ×2
list ×2
data.table ×1
excel ×1
matrix ×1
melt ×1
r-package ×1
ubuntu-16.04 ×1
vector ×1