在R中,统计列表中元素的数量,然后将计数作为一个列表。

huangapple go评论73阅读模式
英文:

Counting the number of elements in a list and then taking the counting as a list in R

问题

I have a vector:

x <- c(0.8,1.0,661.7,661.8,661.9,662.3,662.6,662.7,663.3,663.6,663.7)

I have used function as.data.frame(x) to make the following data frame:

X1
1 0.8
2 1.0
3 661.7
4 661.8
5 661.9
6 662.3
7 662.6
8 662.7
9 663.3
10 663.6
11 663.7

How can I take the list of labels for each element?

i.e x0 <- c(1,2,3,4,5,6,7,8,9,10,11)

英文:

I have a vector:

x &lt;- c(0.8,1.0,661.7,661.8,661.9,662.3,662.6,662.7,663.3,663.6,663.7)

I have used function as.data.frame(x) to make following data frame:

       X1
1      0.8
2      1.0
3    661.7
4    661.8
5    661.9
6    662.3
7    662.6
8    662.7
9    663.3
10   663.6
11   663.7

How can I take the list of labels for each element?

i.e x0 &lt;- c(1,2,3,4,5,6,7,8,9,10,11)

答案1

得分: 2

我们可以有一个名为vector的变量,并使用stack,这也可以灵活地具有不同的标签。

或者使用rownames_to_column

library(dplyr)
as.data.frame(x) %>%
     rownames_to_column('rn')
英文:

We can have a named vector and use stack and this is also flexible in having different labels

stack(setNames(x, seq_along(x)))

Or using rownames_to_column

library(dplyr)
as.data.frame(x) %&gt;%
     rownames_to_column(&#39;rn&#39;)

答案2

得分: 1

If you already created that dataframe, take out the rownames:

x <- c(0.8,1.0,661.7,661.8,661.9,662.3,662.6,662.7,663.3,663.6,663.7)
rownames(as.data.frame(x))
#>  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11"
英文:

If you already created that dataframe, take out the rownames:

x &lt;- c(0.8,1.0,661.7,661.8,661.9,662.3,662.6,662.7,663.3,663.6,663.7)
rownames(as.data.frame(x))
#&gt;  [1] &quot;1&quot;  &quot;2&quot;  &quot;3&quot;  &quot;4&quot;  &quot;5&quot;  &quot;6&quot;  &quot;7&quot;  &quot;8&quot;  &quot;9&quot;  &quot;10&quot; &quot;11&quot;

huangapple
  • 本文由 发表于 2020年1月7日 00:53:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定