我将工作目录中的所有文本文件读入列表,并剪切了一些列
all.files <- list.files(pattern = ".*.txt")
data.list <- lapply(all.files, function(x)read.table(x, sep="\t"))
names(data.list) <- all.files
data.list <- lapply(data.list, function(x) x[,1:3])
Run Code Online (Sandbox Code Playgroud)
我最终得到了"2的清单"
> str(data.list)
List of 2
$ 001.txt:'data.frame': 71330 obs. of 3 variables:
..$ V1: Factor w/ 71321 levels
..$ V2: Factor w/ 1382 levels
..$ V3: num [1:71330] 89.1 99.5 98.8 99.4 99.5 ...
$ 002.txt:'data.frame': 98532 obs. of 3 variables
..$ V1: Factor w/ 98517 levels
..$ V2: Factor w/ 1348 levels
..$ V3: num [1:98532] 99.5 99 99.5 …Run Code Online (Sandbox Code Playgroud) library(ggplot2)
##
df <- as.data.frame(matrix(rnorm(60*2, mean=3,sd=1), 60, 2))
colnames(df) <- c("A", "B")
cf1 <- coef(lm(B~A, data=df))
##
ggplot(df, aes(A,B)) +
geom_point() +
stat_smooth(method = "lm", color="red", fill="red", alpha=0.1, fullrange=TRUE) +
#xlim(0,6)+
geom_abline(intercept = cf1[1], slope = cf1[2], lty="dashed", col="green")
Run Code Online (Sandbox Code Playgroud)
我想将geom_line限制为与stat_smooth相同的范围(似乎由xmax / xmin定义)。xlim参数没有帮助(这是在此处提出的)。在实际应用中,geom_line的斜率和截距将从模型更新中提取,因此它们会略有不同。谢谢。
从一个非常简单的数据框架
time1 <- as.Date("2010/10/10")
time2 <- as.Date("2010/10/11")
time3 <- as.Date("2010/10/12")
test <- data.frame(Sample=c("A","B", "C"), Date=c(time1, time2, time3))
Run Code Online (Sandbox Code Playgroud)
我如何获得一个具有成对的样本A,样本B,样本C之间的时间间隔(样本之间经过的天数)的矩阵?
A B C
A 0 1 2
B 1 0 1
C 2 1 0
Run Code Online (Sandbox Code Playgroud)
/ edit:更改日期的格式。不便之处,敬请原谅
我有一个数据框列表:
lists <- replicate(10, as.data.frame(matrix(rnorm(100), 10, 10)), simplify = FALSE)
names(lists) <- LETTERS[1:10]
Run Code Online (Sandbox Code Playgroud)
我想将所有数据帧写入一个单独的文件,根据它们在 R 中的对象名称命名。我尝试lapply使用paste,但这非常失败:
lapply(lists, function(x) write.table(x, file=paste(x,".txt"), sep="\t"))
Error in file(file, ifelse(append, "a", "w")) :
invalid 'description' argument
In addition: Warning message:
In if (file == "") file <- stdout() else if (is.character(file)) { :
the condition has length > 1 and only the first element will be used
Called from: file(file, ifelse(append, "a", "w"))
Run Code Online (Sandbox Code Playgroud)
如何在不为 n 个 data.frames 编写 n 单行的情况下执行此操作?
我有一个包含 DNA 序列字符串的向量:
x <- c("ATTAGCCGAGC", "TTCCGGTTAA")
Run Code Online (Sandbox Code Playgroud)
我想根据规则将这些字符串转换为总和
A <- 2
T <- 2
G <- 4
C <- 4
Run Code Online (Sandbox Code Playgroud)
这样 ATTAGCCGAGC 就会被翻译为“2+2+2+2+4+4+4+4+2+4+4”,最终输出将是“34”。
期望的输出:由原始向量 X 的一列和“总和变换”的另一列组成的数据帧。
谢谢。
我希望使用“T”不会有问题。
抱歉,我有一个基本问题,但我有一个元素列表,它们是数值向量
str(list1)
List of 34
$ 1 : num [1:2037171] 98.3 98.2 98.1 97.4 97.9 98 97.7 98.1 98.4 98 ...
$ 3 : num [1:692076] 98.8 98.1 97.6 96.6 96.4 96.9 96.1 95.8 96.7 96.5 ...
$ 2 : num [1:82621] 97.7 97.7 97.4 97.7 98.4 98.1 97.4 98 97.6 98.3 ..
.
.
.
Run Code Online (Sandbox Code Playgroud)
,似乎 list.sort(或 order) 不起作用,因为 list1 不是原子向量。我想按 list1 向量的长度对 list1 进行排序。这怎么可能?很抱歉“滥用”这个网站作为我的个人 R 教程。在谷歌上找不到答案。
我仍在为我的地块的正确标签而苦苦挣扎。我设法让它发挥作用。
df <-data.frame(a=seq(1,20),b=seq(1,20))
ggplot(df, aes(x = a, y = b)) + geom_point() +
ylab(expression(paste(CH[4]~" formation rate"~"(",mu, mol, day^{-1},")")))
Run Code Online (Sandbox Code Playgroud)
从这个数据框
df <- data.frame(cat=c(rep("X", 20),rep("Y", 20), rep("Z",20)),
value=c(runif(20),runif(20)*100, rep(0, 20)),
var=rep(LETTERS[1:5],12))
Run Code Online (Sandbox Code Playgroud)
我想创建多面箱线图。
library(ggplot2)
p1 <- ggplot(df, aes(var,value)) + geom_boxplot() + facet_wrap(~cat, scale="free")
p1
Run Code Online (Sandbox Code Playgroud)
结果在美观上并不令人满意,因为它将空面板的 y 轴中心为零。我想将所有 y 尺度从零开始。我从之前的问题中尝试了几个答案:
p1 + scale_y_continuous(expand = c(0, 0)) # not working
p1 + expand_limits(y = 0) #not working
p1 + scale_y_continuous(limits=c(0,NA)) ## not working
p1 + scale_y_continuous(limits=c(0,100)) ## partially working, but defeats scale="free"
p1 + scale_y_continuous(limits=c(0,max(df$value))) ## partially working, see above
p1 + scale_y_continuous(limits=c(0,max(df$value))) + expand_limits(y = 0)## partially working, see above …Run Code Online (Sandbox Code Playgroud) 当运行 4.0 之前的代码时,我收到此消息(德语)。
\n Warnung: Namensraum \xe2\x80\x98gamlss.dist\xe2\x80\x99 ist nicht verf\xc3\xbcgar und ist durch.GlobalEnv ersetzt worden w\xc3\xa4hrend Objekt \xe2\x80\x98mod_gam\xe2\x80\x99 verarbeitet wurde\nRun Code Online (Sandbox Code Playgroud)\n翻译:
\n"Warning: Namespace "gamlss.dist" is not available and is replaced by .GlobalEnv, while object "mod_gam" was processed."\nRun Code Online (Sandbox Code Playgroud)\n我重新安装了gamboostlss、gamboost和mboost,但这个警告仍然存在。我怎样才能摆脱它,我可以相信计算结果吗?
\n在这个数据集中,两个分类群(在行中)对整体数据的贡献很小,我想收集所有这些行,其行和小于整个数据集的 n%。n 可以是 1、2、3...
df <- data.frame(A=c(1000,100,1,0), B=c(100,1000,1,1), C=c(10,900,0,1))
row.names(df) <- c("Tax1", "Tax2", "Tax3", "Tax4")
> df
A B C
Tax1 1000 100 10
Tax2 100 1000 900
Tax3 1 1 0
Tax4 0 1 1
Run Code Online (Sandbox Code Playgroud)
在确定这些低总和行后,我想将它们归类为“其他”:
> df
A B C
Tax1 1000 100 10
Tax2 100 1000 900
Other 1 2 1
Run Code Online (Sandbox Code Playgroud)
谢谢!