Python和R是否有不同的方法来计算标准差(sd)?
例如,在python中,以开头:
a = np.array([[1,2,3], [4,5,6], [7,8,9]])
print(a.std(axis=1))
### per row : [0.81649658 0.81649658 0.81649658]
print(a.std(axis=0))
### per column : [2.44948974 2.44948974 2.44948974]
Run Code Online (Sandbox Code Playgroud)
在R中:
z <- matrix(c(1,2,3,4,5,6,7,8,9), nrow=3, ncol=3, byrow=T)
# z
# [,1] [,2] [,3]
#[1,] 1 2 3
#[2,] 4 5 6
#[3,] 7 8 9
# apply(z, 1, sd)
sd(z[1,]) #1
sd(z[2,]) #1
sd(z[3,]) #1
# apply(z, 2, sd)
sd(z[,1]) #3
sd(z[,2]) #3
sd(z[,3]) #3
Run Code Online (Sandbox Code Playgroud) 我很感激您对子集数据框的建议.让我们考虑一个示例数据框df:
dd <- c(1,2,3)
rows <- c("A1","A2","A3")
columns <- c("B1","B2","B3")
numbers <- c(400, 500, 600)
df <- dataframe(dd, rows, columns, numbers)
Run Code Online (Sandbox Code Playgroud)
和矢量: test_rows <-c("A1","A3")
我如何以这样一种方式df对向量的数据帧功能进行子集化,test_rows即只列出与("A1"和"A3")元素匹配的数据帧df(df$rows)行test_rows?