将dataframe转换为每行列表,在R中每行创建一个向量
我有以下数据帧df(实际上要大得多): 我需要把它变成一个物体,这样结构看起来像这样将dataframe转换为每行列表,在R中每行创建一个向量,r,list,dataframe,vector,type-conversion,R,List,Dataframe,Vector,Type Conversion,我有以下数据帧df(实际上要大得多): 我需要把它变成一个物体,这样结构看起来像这样 str(df) List of 3 $ : chr [1:3] "ORp0" "RT1p20" "RT2p20" $ : chr [1:3] "RT3p0" "RT4p0" "RT5p0" $ : chr [1:3] "RT3p0" "RT4p0&qu
str(df)
List of 3
$ : chr [1:3] "ORp0" "RT1p20" "RT2p20"
$ : chr [1:3] "RT3p0" "RT4p0" "RT5p0"
$ : chr [1:3] "RT3p0" "RT4p0" "RT5p05"
似乎这个列表的每一行都是一个向量。如何从数据帧实现这种转换
非常感谢试试这个:
#Data
df <- structure(list(A = c("ORp0", "RT3p0", "RT3p0"), B = c("RT1p20",
"RT4p0", "RT4p0"), C = c("RT2p20", "RT5p0", "RT5p05")), class = "data.frame", row.names = c("1",
"2", "3"))
如果要进一步格式化,可以使用下一个代码:
L2 <- lapply(List, function(x) {y <- c(t(x[1,])); return(y)})
和你想要的差不多。正如观察一样,请小心使用行名,并检查它们是否从数据帧中的1个观察开始到最后一个观察。有时候他们会不一样 试试这个:
#Data
df <- structure(list(A = c("ORp0", "RT3p0", "RT3p0"), B = c("RT1p20",
"RT4p0", "RT4p0"), C = c("RT2p20", "RT5p0", "RT5p05")), class = "data.frame", row.names = c("1",
"2", "3"))
如果要进一步格式化,可以使用下一个代码:
L2 <- lapply(List, function(x) {y <- c(t(x[1,])); return(y)})
和你想要的差不多。正如观察一样,请小心使用行名,并检查它们是否从数据帧中的1个观察开始到最后一个观察。有时候他们会不一样 您可以使用
split
:
new_list <- split(as.matrix(df), row(df))
new_list
$`1`
[1] "ORp0" "RT1p20" "RT2p20"
$`2`
[1] "RT3p0" "RT4p0" "RT5p0"
$`3`
[1] "RT3p0" "RT4p0" "RT5p05"
str(new_list)
List of 3
$ 1: chr [1:3] "ORp0" "RT1p20" "RT2p20"
$ 2: chr [1:3] "RT3p0" "RT4p0" "RT5p0"
$ 3: chr [1:3] "RT3p0" "RT4p0" "RT5p05"
new\u list您可以使用split
:
new_list <- split(as.matrix(df), row(df))
new_list
$`1`
[1] "ORp0" "RT1p20" "RT2p20"
$`2`
[1] "RT3p0" "RT4p0" "RT5p0"
$`3`
[1] "RT3p0" "RT4p0" "RT5p05"
str(new_list)
List of 3
$ 1: chr [1:3] "ORp0" "RT1p20" "RT2p20"
$ 2: chr [1:3] "RT3p0" "RT4p0" "RT5p0"
$ 3: chr [1:3] "RT3p0" "RT4p0" "RT5p05"
新建列表您可以尝试以下选项:
asplit
+unname
或者更简单的一个(注释中的@akrun)
unclass
+as.data.frame
+t
您可以尝试以下选项:
asplit
+unname
或者更简单的一个(注释中的@akrun)
unclass
+as.data.frame
+t
您可以使用asplit(unname(df),1)
@akrun谢谢!你的是简洁的:)你可以使用asplit(unname(df),1)
@akrun谢谢!你的话很简洁:)
new_list <- split(as.matrix(df), row(df))
new_list
$`1`
[1] "ORp0" "RT1p20" "RT2p20"
$`2`
[1] "RT3p0" "RT4p0" "RT5p0"
$`3`
[1] "RT3p0" "RT4p0" "RT5p05"
str(new_list)
List of 3
$ 1: chr [1:3] "ORp0" "RT1p20" "RT2p20"
$ 2: chr [1:3] "RT3p0" "RT4p0" "RT5p0"
$ 3: chr [1:3] "RT3p0" "RT4p0" "RT5p05"
> str(Map(unname,asplit(df,1)))
List of 3
$ 1: chr [1:3(1d)] "ORp0" "RT1p20" "RT2p20"
$ 2: chr [1:3(1d)] "RT3p0" "RT4p0" "RT5p0"
$ 3: chr [1:3(1d)] "RT3p0" "RT4p0" "RT5p05"
> str(asplit(unname(df),1))
List of 3
$ 1: chr [1:3(1d)] "ORp0" "RT1p20" "RT2p20"
..- attr(*, "dimnames")=List of 1
.. ..$ : NULL
$ 2: chr [1:3(1d)] "RT3p0" "RT4p0" "RT5p0"
..- attr(*, "dimnames")=List of 1
.. ..$ : NULL
$ 3: chr [1:3(1d)] "RT3p0" "RT4p0" "RT5p05"
..- attr(*, "dimnames")=List of 1
.. ..$ : NULL
- attr(*, "dim")= int 3
- attr(*, "dimnames")=List of 1
..$ : chr [1:3] "1" "2" "3"
> str(unclass(as.data.frame(t(df))))
List of 3
$ 1: chr [1:3] "ORp0" "RT1p20" "RT2p20"
$ 2: chr [1:3] "RT3p0" "RT4p0" "RT5p0"
$ 3: chr [1:3] "RT3p0" "RT4p0" "RT5p05"
- attr(*, "row.names")= chr [1:3] "A" "B" "C"