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

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

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:

  1. 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:

  1. X1
  2. 1 0.8
  3. 2 1.0
  4. 3 661.7
  5. 4 661.8
  6. 5 661.9
  7. 6 662.3
  8. 7 662.6
  9. 8 662.7
  10. 9 663.3
  11. 10 663.6
  12. 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

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

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

  1. stack(setNames(x, seq_along(x)))

Or using rownames_to_column

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

答案2

得分: 1

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

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

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

  1. 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)
  2. rownames(as.data.frame(x))
  3. #&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:

确定