我有一个像这样的列表
像这样的玩具数据
ltd <- list(structure(list(Abund = c("BROS", "KIS", "TTHS",
"MKS"), `Value: F111: cold, Sample1` = c("1.274e7", "",
"", "2.301e7"), `Value: F111: warm, Sample1` = c("", "",
"", "")), .Names = c("Abund", "Value: F111: cold, Sample1",
"Value: F111: warm, Sample1"), row.names = c(NA, 4L), class = "data.frame"),
structure(list(Abund = c("BROS", "TMS", "KIS",
"HERS"), `Value: F216: cold, Sample2` = c("1.670e6",
"4.115e7", "", "1.302e7"), `Value: F216: warm, Sample2` = c("",
"2.766e7", "", "1.396e7")), .Names = c("Abund", "Value: F216: cold, Sample2",
"Value: F216: warm, Sample2"), row.names = c(NA, 4L), class = "data.frame"),
structure(list(Abund = c("BROS", "TMS", "KIS",
"HERS"), `Value: F655: cold, Sample3` = c("7.074e4",
"1.038e7", "", "7.380e5"), `Value: F655: warm, Sample3` = c("",
"6.874e6", "", "7.029e5")), .Names = c("Abund", "Value: F655: cold, Sample3",
"Value: F655: warm, Sample3"), row.names = c(NA, 4L), class = "data.frame"))
List of 5000
$ :'data.frame': 397 obs. of 3 variables:
..$ Abund : chr [1:363] "TTT" "MMM" "GTR" "NLM" ...
..$ Value: F111: Warm, Sample1: chr [1:363] "1.274e7" "" "" "2.301e7" ...
..$ Value: F111: Cold, Sample1: chr [1:363] "" "" "" "" ...
$ :'data.frame': 673 obs. of 3 variables:
..$ Abund : chr [1:673] "MGL" "KKK" "LFT" "NKL" ...
..$ Value: F216: Warm, Sample2: chr [1:673] "1.670e6" "4.115e7" "" "1.302e7" ...
..$ Value: F216: Cold, Sample2: chr [1:673] "" "2.766e7" "" "1.396e7" ...
$ :'data.frame': 779 obs. of 3 variables:
..$ Abund : chr [1:779] "TTLS" "KIS" "KISA" "LISU" ...
..$ Value: F655: Warm, Sample3: chr [1:779] "7.074e4" "1.038e7" "" "7.380e5" ...
..$ Value: F655: Cold, Sample3: chr [1:779] "" "6.874e6" "" "7.029e5" ...
$ :'data.frame': 387 obs. of 3 variables:
..$ Abund : chr [1:387] "BRO" "BIA" "KIA" "TTHS" ...
..$ Value: F57: Warm, Sample4: chr [1:387] "6.910e6" "" "2.435e7" "3.924e6" ...
..$ Value: F57: Cold, Sample4: chr [1:387] "5.009e6" "" "" "3.624e6" ...
$ :'data.frame': 543 obs. of 3 variables:
我想给从1到它的所有内容赋予丰富的唯一名称,因此输出应该看起来像
所以disire输出如下所示。我必须写一个等等等等,这个网站允许我发布我的问题,否则它不允许
List of 5000
$ :'data.frame': 397 obs. of 3 variables:
..$ Abund1 : chr [1:363] "TTT" "MMM" "GTR" "NLM" ...
..$ Value: F111: Warm, Sample1: chr [1:363] "1.274e7" "" "" "2.301e7" ...
..$ Value: F111: Cold, Sample1: chr [1:363] "" "" "" "" ...
$ :'data.frame': 673 obs. of 3 variables:
..$ Abund2 : chr [1:673] "MGL" "KKK" "LFT" "NKL" ...
..$ Value: F216: Warm, Sample2: chr [1:673] "1.670e6" "4.115e7" "" "1.302e7" ...
..$ Value: F216: Cold, Sample2: chr [1:673] "" "2.766e7" "" "1.396e7" ...
$ :'data.frame': 779 obs. of 3 variables:
..$ Abund3 : chr [1:779] "TTLS" "KIS" "KISA" "LISU" ...
..$ Value: F655: Warm, Sample3: chr [1:779] "7.074e4" "1.038e7" "" "7.380e5" ...
..$ Value: F655: Cold, Sample3: chr [1:779] "" "6.874e6" "" "7.029e5" ...
$ :'data.frame': 387 obs. of 3 variables:
..$ Abund4 : chr [1:387] "BRO" "BIA" "KIA" "TTHS" ...
..$ Value: F57: Warm, Sample4: chr [1:387] "6.910e6" "" "2.435e7" "3.924e6" ...
..$ Value: F57: Cold, Sample4: chr [1:387] "5.009e6" "" "" "3.624e6" ...
答案 0 :(得分:1)
要解决这样的问题,不要事先解决大问题,最好一次解决一个问题。如果我们只查看列表中的一个框架,我会将其称为x
:
x <- structure(list(Abund = c("BROS", "KIS", "TTHS",
"MKS"), `Value: F111: cold, Sample1` = c("1.274e7", "",
"", "2.301e7"), `Value: F111: warm, Sample1` = c("", "",
"", "")), .Names = c("Abund", "Value: F111: cold, Sample1",
"Value: F111: warm, Sample1"), row.names = c(NA, 4L), class = "data.frame")
str(x)
# 'data.frame': 4 obs. of 3 variables:
# $ Abund111 : chr "BROS" "KIS" "TTHS" "MKS"
# $ Value: F111: cold, Sample1: chr "1.274e7" "" "" "2.301e7"
# $ Value: F111: warm, Sample1: chr "" "" "" ""
你本来想在&#34; F&#34;之后追加这个数字。在其他列名称中。我先攻击那个,然后如果你真的想要它,我也会做&#34;追加一个递增的数字&#34;的事情。
编写一个找到&#34; F&#34;的函数。第二列名称中的数字,并将其附加到第一列名称。 (我想知道你的完整数据集中是否有更多不同的标题模式;我确信我们在这里使用的正则表达式很容易被操作来处理它们,给定足够多的样本。)
somefunc <- function(x) {
cn2 <- colnames(x)[2]
Fnum <- gsub(".*F([0-9]+).*", "\\1", cn2)
colnames(x)[1] <- paste0(colnames(x)[1], Fnum)
x
}
简要说明:
colnames(x)[2]
只是检索第二个;我假设我们可以将所有内容建立在第二列的存在和构成上gsub(".*F([0-9]+).*", "\\1", cn2)
只提取&#34; F&#34;之后的数字;为了记录,如果它不是Sample
,我们可能会丢弃任何非数字,但我选择在这里安全。
.*
匹配零个或多个&#34;任何&#34;字符;把剩下的东西夹在我们小组的两边,实质上是丢弃了我们想要的所有数字F
字面意思&#34; F&#34; (...)
这是一个组,保存以供日后使用(在替换字符串中引用\\1
,第二个参数引用到gsub
)[0-9]+
接受括号内的任何内容,可以是文字([acf]
匹配三个字母)或范围([0-9A-F]
匹配任何数字和A和F之间的任何字母); +
使其成为&#34;一个或多个&#34; (与*
之前的对比为零或更多)colnames(x)[1] <- ...
重新分配第一个列名称关于&#34;单帧的工作&#34;:
str( somefunc(x) )
# 'data.frame': 4 obs. of 3 variables:
# $ Abund111 : chr "BROS" "KIS" "TTHS" "MKS"
# $ Value: F111: cold, Sample1: chr "1.274e7" "" "" "2.301e7"
# $ Value: F111: warm, Sample1: chr "" "" "" ""
所以现在的问题是如何在一个帧列表中应用这个在一帧上运行的函数。 lapply
救援:
str(lapply(ltd, somefunc))
# List of 3
# $ :'data.frame': 4 obs. of 3 variables:
# ..$ Abund111 : chr [1:4] "BROS" "KIS" "TTHS" "MKS"
# ..$ Value: F111: cold, Sample1: chr [1:4] "1.274e7" "" "" "2.301e7"
# ..$ Value: F111: warm, Sample1: chr [1:4] "" "" "" ""
# $ :'data.frame': 4 obs. of 3 variables:
# ..$ Abund216 : chr [1:4] "BROS" "TMS" "KIS" "HERS"
# ..$ Value: F216: cold, Sample2: chr [1:4] "1.670e6" "4.115e7" "" "1.302e7"
# ..$ Value: F216: warm, Sample2: chr [1:4] "" "2.766e7" "" "1.396e7"
# $ :'data.frame': 4 obs. of 3 variables:
# ..$ Abund655 : chr [1:4] "BROS" "TMS" "KIS" "HERS"
# ..$ Value: F655: cold, Sample3: chr [1:4] "7.074e4" "1.038e7" "" "7.380e5"
# ..$ Value: F655: warm, Sample3: chr [1:4] "" "6.874e6" "" "7.029e5"
这既容易又困难。首先,我们解决这个小问题:
otherfunc <- function(x, num) {
colnames(x)[1] <- paste0(colnames(x)[1], num)
x
}
非常直接。但是我们不能使用lapply
:它只接受一个参数,所以它不知道该怎么做。有人可能会试图用跟踪变量在某个地方蛮力(全局?请不要),但知道有一个&#34; apply&#34;的变体可能会很有趣。以不同方式运作的函数:mapply
需要一个或多个列表,以及&#34; zips&#34;他们在一起。例如:
myfunc <- c
mapply(myfunc, 1:3, 4:6, 7:9, SIMPLIFY=FALSE)
# [[1]]
# [1] 1 4 7
# [[2]]
# [1] 2 5 8
# [[3]]
# [1] 3 6 9
我们从三个(可能更多)独立向量开始(可能是列表,通常是列表),并从每个向量中获取第一个值并将它们传递给函数。所以这实际上就像:
list(myfunc(1, 4, 7), mufunc(2, 5, 8), myfunc(3, 6, 9))
好的,所以意识到我们想要&#34; zip&#34;将ltd
的每个帧与序列中的数字放在一起,这些数字很容易生成:
seq_along(ltd)
# [1] 1 2 3
(这被认为优于1:length(ltd)
,因为如果长度为0,后者将无法正常运行...尝试1:length(list())
与seq_along(list())
。)
好的,让我们使用这个新技巧:
str(mapply(otherfunc, ltd, seq_along(ltd), SIMPLIFY=FALSE))
# List of 3
# $ :'data.frame': 4 obs. of 3 variables:
# ..$ Abund1 : chr [1:4] "BROS" "KIS" "TTHS" "MKS"
# ..$ Value: F111: cold, Sample1: chr [1:4] "1.274e7" "" "" "2.301e7"
# ..$ Value: F111: warm, Sample1: chr [1:4] "" "" "" ""
# $ :'data.frame': 4 obs. of 3 variables:
# ..$ Abund2 : chr [1:4] "BROS" "TMS" "KIS" "HERS"
# ..$ Value: F216: cold, Sample2: chr [1:4] "1.670e6" "4.115e7" "" "1.302e7"
# ..$ Value: F216: warm, Sample2: chr [1:4] "" "2.766e7" "" "1.396e7"
# $ :'data.frame': 4 obs. of 3 variables:
# ..$ Abund3 : chr [1:4] "BROS" "TMS" "KIS" "HERS"
# ..$ Value: F655: cold, Sample3: chr [1:4] "7.074e4" "1.038e7" "" "7.380e5"
# ..$ Value: F655: warm, Sample3: chr [1:4] "" "6.874e6" "" "7.029e5"
应该注意mapply
,就像sapply
一样,默认会尝试简化事情;我发现很难相信它总能做我想要的,所以我通常会关闭这种简化。有时候,是的,现在不是那个时候。在使用线性/迭代方法进行思考时,apply
函数(包括Reduce
)通常很难学会使用,但它们在这些时候非常有用。
答案 1 :(得分:0)
在基地R你可以这样做:
function insertParam(key,value) {
if (history.pushState) {
var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?' +key+'='+value;
window.history.pushState({path:newurl},'',newurl);
}
}
但我会使用ltd2 <- Map(function(x,y) {names(x)[1] <- paste0(names(x)[1],y);x},ltd,seq(ltd))
str(ltd2)
# List of 3
# $ :'data.frame': 4 obs. of 3 variables:
# ..$ Abund1 : chr [1:4] "BROS" "KIS" "TTHS" "MKS"
# ..$ Value: F111: cold, Sample1: chr [1:4] "1.274e7" "" "" "2.301e7"
# ..$ Value: F111: warm, Sample1: chr [1:4] "" "" "" ""
# $ :'data.frame': 4 obs. of 3 variables:
# ..$ Abund2 : chr [1:4] "BROS" "TMS" "KIS" "HERS"
# ..$ Value: F216: cold, Sample2: chr [1:4] "1.670e6" "4.115e7" "" "1.302e7"
# ..$ Value: F216: warm, Sample2: chr [1:4] "" "2.766e7" "" "1.396e7"
# $ :'data.frame': 4 obs. of 3 variables:
# ..$ Abund3 : chr [1:4] "BROS" "TMS" "KIS" "HERS"
# ..$ Value: F655: cold, Sample3: chr [1:4] "7.074e4" "1.038e7" "" "7.380e5"
# ..$ Value: F655: warm, Sample3: chr [1:4] "" "6.874e6" "" "7.029e5"
和purrr::imap
获得相同的结果:
dplyr::rename_at