将行名char(X1,X2,... Xn)转换为num(1,2,... n)

时间:2019-08-29 14:21:22

标签: r dataframe

我创建了一个新的数据框,其行名的命名类似于char(X1,X2,X3,... Xn)。

为了将新的数据帧与旧的数据帧合并,我需要它们为num(1,2,3,...,n)。

# Create DB with Topics
df_test <- data.frame(doc_topic_distr)
setDT(df_test, keep.rownames = "doc_id")

此后,我尝试df_test$doc_id <- as.integer(gsub('[a-zA-Z]', '', df$doc_id))对其进行操作,但这没有用。 :/

这有什么线索吗?

/ e:我们去了:

> df_test$doc_id <- gsub('[a-zA-Z]', '', df$doc_id)
Error in df$doc_id : object of type 'closure' is not subsettable
> 
> dput(head(doc_topic_distr))
structure(c(0, 0, 0, 0, 0, 0.037037037037037, 0, 0.08, 0, 0, 
0, 0, 0.25, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.32, 
0, 0, 0.875, 0.407407407407407, 0, 0.16, 0, 0.166666666666667, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0.04, 0, 0, 0, 0.0740740740740741, 0, 0.12, 
0, 0, 0, 0.037037037037037, 0, 0.04, 0, 0, 0, 0, 0, 0.08, 0, 
0, 0, 0.0740740740740741, 0.25, 0, 0, 0, 0.0625, 0.037037037037037, 
0, 0, 0, 0.333333333333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0.222222222222222, 0, 0, 0, 0, 0, 0.037037037037037, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0625, 0, 0, 0, 0, 0, 0, 0.037037037037037, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.037037037037037, 0, 0, 0, 
0, 0, 0, 0, 0.16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(6L, 
31L), .Dimnames = list(c("0", "1", "2", "3", "4", "5"), NULL))
´´´
Many thanks in advance!

1 个答案:

答案 0 :(得分:0)

像这样解决它:

marshal_with