以下代码给了我数据表
library(RMySQL)
library(reshape)
library(philentropy)
library(distances)
mydb = dbConnect(MySQL(), user='root', password='root', dbname='test_db', host='127.0.0.1')
rs = dbSendQuery(mydb,'select cv.entity_id,cv.attribute_id, cv.value/1000 as value from test_1 cv limit 100')
data = fetch(rs,n=-1)
pivotedData = cast(data,entity_id ~ attribute_id)
distCalcNew = distances(pivotedData,id_variable='entity_id')
nns <- nearest_neighbor_search(distCalcNew,k=3)
nnsdt <- data.table(nns)
我在R中有一个数据表,如下所示,数据点指示列索引
8456 8720 5780
1: 1 2 3
2: 3 3 2
3: 2 1 1
是否可能获得以下信息?
8456 8720 5780
1: 8456 8720 5780
2: 5780 5780 8720
3: 8720 8456 8456
对不起,我是R的新手
答案 0 :(得分:2)
这是另一个想法,
m1 <- matrix(names(x)[unlist(x)], ncol = ncol(x))
#tidy up
setNames(data.frame(m1), names(x))
# 8456 8720 5780
#1 8456 8720 5780
#2 5780 5780 8720
#3 8720 8456 8456
答案 1 :(得分:1)
您可以对数据框python 3.5
使用lapply
,以便根据该值检索该索引中的列名
df
答案 2 :(得分:1)
使用 lapply ,但必须有更好的“ data.table-way”:
library(data.table)
x <- fread("8456 8720 5780
1 2 3
3 3 2
2 1 1", header = TRUE)
as.data.table(lapply(x, function(i) as.integer(colnames(x)[ i ])))
# 8456 8720 5780
# 1: 8456 8720 5780
# 2: 5780 5780 8720
# 3: 8720 8456 8456