我有一个频率表,计算向量中元素的频率
a = table(c(0,1,1,1,0,2,2,4,1,2,3,2,1,2,3,1,1,1,2,3,4,1,1,0))
a
# 0 1 2 3 4
# 3 10 6 3 2
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过名字(a)访问该名称.但是当我试图访问SECOND行的值时
a[1, "0"]
# Error in a[1, "0"] : incorrect number of dimensions
a[1, 1]
# Error in a[1, 1] : incorrect number of dimensions
Run Code Online (Sandbox Code Playgroud) 为什么table函数找到一个被删除的变量?
Dog <- c("Rover", "Spot")
Cat <- c("Scratch", "Fluffy")
Pets <- data.frame(Dog, Cat) #create a data frame with two variables
names(Pets)
# [1] "Dog" "Cat"
#rename Dog to a longer name
names(Pets)[names(Pets)=="Dog"] <- "Dog_as_very_long_name"
Pets$Dog <- NULL # delete Dog
names(Pets)
#[1] "Dog_as_very_long_name" "Cat" #the variable dog is not in the data set anymore
table(Pets$Dog) #Why does the table function on a variable that was deleted
# Rover Spot
# 1 1
Run Code Online (Sandbox Code Playgroud) 我想创建一个简单的表,展示我的数据集中给定变量的最大10个值,以及每个观察的4个其他变量,所以基本上是我的数据的一小部分.它看起来像这样:
Score District Age Group Gender
17 B 23 Red 1
12 A 61 Red 0
11.7 A 18 Blue 0
10 B 18 Red 0
.
.
etc.
Run Code Online (Sandbox Code Playgroud)
从而在Score var上对数据进行排序.所有数据都包含在同一数据帧中.
A = c(1,2,3,2,1,2,2,2,1,2,3,2,1)
B = c(2,3,2,3,2,2,1,1,2,1,2,2,3)
mytable = table(A,B)
Run Code Online (Sandbox Code Playgroud)
找回两个向量的最佳解决方案是什么mytable?当然,它不会是完全相同的向量,但必须遵守A相比的顺序B.是否有意义?