Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将data.frame中的列命名为DataFrame列表中数据帧的名称_R_List_Dataframe - Fatal编程技术网

如何将data.frame中的列命名为DataFrame列表中数据帧的名称

如何将data.frame中的列命名为DataFrame列表中数据帧的名称,r,list,dataframe,R,List,Dataframe,我有一个数据帧列表;每个数据帧具有相同的列名和相同的列数(2): 以此类推,最多12个数据帧 我现在要做的是将数据帧中第二列的名称(“read_count”)更改为数据帧的名称。 看起来像这样: List of 12 $ AM1:'data.frame': 1978 obs. of 2 variables: ..$ miRNA : chr [1:1978] "let-7a-1-3p" "let-7a-2-3p" "let-7

我有一个数据帧列表;每个数据帧具有相同的列名和相同的列数(2):

以此类推,最多12个数据帧

我现在要做的是将数据帧中第二列的名称(“read_count”)更改为数据帧的名称。 看起来像这样:

List of 12
 $ AM1:'data.frame':    1978 obs. of  2 variables:
  ..$ miRNA     : chr [1:1978] "let-7a-1-3p" "let-7a-2-3p" "let-7a-5p" "let-7b-3p" ...
  ..$ AM1: num [1:1978] 1383 0 396731 40 5889 ...
 $ AM2:'data.frame':    1978 obs. of  2 variables:
  ..$ miRNA     : chr [1:1978] "let-7a-1-3p" "let-7a-2-3p" "let-7a-5p" "let-7b-3p" ...
  ..$ AM2: num [1:1978] 930 0 293379 24 3051 ...
 $ AM3:'data.frame':    1978 obs. of  2 variables:
  ..$ miRNA     : chr [1:1978] "let-7a-1-3p" "let-7a-2-3p" "let-7a-5p" "let-7b-3p" ...
  ..$ AM3: num [1:1978] 1321 0 408655 23 2353 ...

当然,我们的想法不是用类似的东西手动执行,您可以用这种样式的循环来执行,使用要分配到数据帧特定名称中的列表名称:

#Loop
for(i in 1:length(read_counts_list))
{
  names(read_counts_list[[i]])[2] <- names(read_counts_list)[i]
}

您可以使用这种样式的循环,使用要在数据帧的特定名称中指定的列表名称:

#Loop
for(i in 1:length(read_counts_list))
{
  names(read_counts_list[[i]])[2] <- names(read_counts_list)[i]
}

您可以将
setNames
映射一起使用

LIST <- Map(setNames, LIST, as.data.frame(t(cbind(sapply(LIST, names)[1,], names(LIST)))))

数据:


LIST您可以将
setNames
映射一起使用

LIST <- Map(setNames, LIST, as.data.frame(t(cbind(sapply(LIST, names)[1,], names(LIST)))))

数据:

列表
#Loop
for(i in 1:length(read_counts_list))
{
  names(read_counts_list[[i]])[2] <- names(read_counts_list)[i]
}
#Data
LS <- split(iris,iris$Species)
#Loop
for(i in 1:length(LS))
{
  names(LS[[i]])[5] <- names(LS)[i]
}
LS[[1]]

   Sepal.Length Sepal.Width Petal.Length Petal.Width setosa
1           5.1         3.5          1.4         0.2 setosa
2           4.9         3.0          1.4         0.2 setosa
3           4.7         3.2          1.3         0.2 setosa
4           4.6         3.1          1.5         0.2 setosa
5           5.0         3.6          1.4         0.2 setosa
6           5.4         3.9          1.7         0.4 setosa
7           4.6         3.4          1.4         0.3 setosa
8           5.0         3.4          1.5         0.2 setosa
9           4.4         2.9          1.4         0.2 setosa
10          4.9         3.1          1.5         0.1 setosa
11          5.4         3.7          1.5         0.2 setosa
12          4.8         3.4          1.6         0.2 setosa
13          4.8         3.0          1.4         0.1 setosa
14          4.3         3.0          1.1         0.1 setosa
15          5.8         4.0          1.2         0.2 setosa
16          5.7         4.4          1.5         0.4 setosa
17          5.4         3.9          1.3         0.4 setosa
18          5.1         3.5          1.4         0.3 setosa
19          5.7         3.8          1.7         0.3 setosa
20          5.1         3.8          1.5         0.3 setosa
21          5.4         3.4          1.7         0.2 setosa
22          5.1         3.7          1.5         0.4 setosa
23          4.6         3.6          1.0         0.2 setosa
24          5.1         3.3          1.7         0.5 setosa
25          4.8         3.4          1.9         0.2 setosa
26          5.0         3.0          1.6         0.2 setosa
27          5.0         3.4          1.6         0.4 setosa
28          5.2         3.5          1.5         0.2 setosa
29          5.2         3.4          1.4         0.2 setosa
30          4.7         3.2          1.6         0.2 setosa
31          4.8         3.1          1.6         0.2 setosa
32          5.4         3.4          1.5         0.4 setosa
33          5.2         4.1          1.5         0.1 setosa
34          5.5         4.2          1.4         0.2 setosa
35          4.9         3.1          1.5         0.2 setosa
36          5.0         3.2          1.2         0.2 setosa
37          5.5         3.5          1.3         0.2 setosa
38          4.9         3.6          1.4         0.1 setosa
39          4.4         3.0          1.3         0.2 setosa
40          5.1         3.4          1.5         0.2 setosa
41          5.0         3.5          1.3         0.3 setosa
42          4.5         2.3          1.3         0.3 setosa
43          4.4         3.2          1.3         0.2 setosa
44          5.0         3.5          1.6         0.6 setosa
45          5.1         3.8          1.9         0.4 setosa
46          4.8         3.0          1.4         0.3 setosa
47          5.1         3.8          1.6         0.2 setosa
48          4.6         3.2          1.4         0.2 setosa
49          5.3         3.7          1.5         0.2 setosa
50          5.0         3.3          1.4         0.2 setosa
LIST <- Map(setNames, LIST, as.data.frame(t(cbind(sapply(LIST, names)[1,], names(LIST)))))
str(LIST)
# List of 3
# $ AM1:'data.frame':   5 obs. of  2 variables:
#   ..$ miRNA: num [1:5] 0.7111 -0.9337 -0.0507 -0.4526 1.4833
# ..$ AM1  : num [1:5] 1.382 -0.5125 -0.0438 -1.091 0.8535
# $ AM2:'data.frame':   5 obs. of  2 variables:
#   ..$ miRNA: num [1:5] 0.563 1.256 -1.104 0.367 -0.516
# ..$ AM2  : num [1:5] 0.914 1.308 -0.839 0.403 -1.091
# $ AM3:'data.frame':   5 obs. of  2 variables:
#   ..$ miRNA: num [1:5] 0.548 -1.377 2.179 2.264 0.892
# ..$ AM3  : num [1:5] -0.0564 0.6623 -0.7863 -1.5744 0.3109
LIST <- list(AM1 = structure(list(miRNA = c(-2.51829139109263, -0.877872477016629, 
-0.25969747064056, 1.22571401548266, 0.938000291163749), read_count = c(0.766054639939597, 
-0.748508051788698, 1.2388957678652, -0.169632288961075, 1.60331976703024
)), class = "data.frame", row.names = c(NA, -5L)), AM2 = structure(list(
    miRNA = c(-1.38505365651707, -0.354183187247905, -0.00163202119006995, 
    0.596080469170588, -0.480439453674378), read_count = c(0.46622524539987, 
    -2.06658132516899, -0.448024554783029, -0.371688827763805, 
    0.214663638296237)), class = "data.frame", row.names = c(NA, 
-5L)), AM3 = structure(list(miRNA = c(-0.64657040576356, -1.17865215943876, 
0.181937607228803, 0.186619326022144, -1.26531611982735), read_count = c(-1.20975273029628, 
-0.256061901740592, -0.036373286788934, 0.988560967485261, 0.422093433323588
)), class = "data.frame", row.names = c(NA, -5L)))