我有一个带有列名和行名的数据框。我试图用write.csv和write.table保存数据帧,但是当我在excel中打开csv文件时,列名称的行就在更多行上。如果我在R中重新加载数据帧时没有其他问题,这对我来说并不代表问题。如果我设置header = FALSE,则会发生read.csv重新加载数据的情况,如果我使用header = TRUE,则当我使用view()时,数据帧看起来还不错。但是,如果我尝试从中提取行,它总是给我第一行(带有列名的行)。我试图将列名保存在向量中,从数据集中删除该行,并使用colnames(df)= ..重命名它,但是它不允许我重命名。我能做什么? 首先,我上传了一个csv文件,并对其进行了一些操作
data = read.csv("movie.csv",header=TRUE)
#head(data)
attach(data)
bipartite_network=data[,c("director_name","actor_1_name","actor_2_name","actor_3_name")]
d1=data[,c("director_name","actor_1_name")]
d2=data[,c("director_name","actor_2_name")]
d3=data[,c("director_name","actor_3_name")]
names(d1)=names(d2)
names(d3)=names(d2)
network=rbind(d1,d2)
network=rbind(network,d3)
network=distinct(network)
actors=as.data.frame(network[,2])
actors=distinct(actors)
dim_col=dim(actors)[1][1]
directors=as.data.frame(network[,1])
directors=distinct(directors)
dim_row=dim(directors)[1][1]
adj_matrix=matrix(0,dim_row,dim_col)
colnames(adj_matrix)=actors[,1]
rownames(adj_matrix)=directors[,1]
for(i in 1:(dim(network)[1])){
adj_matrix[network[i,1],network[i,2]]=1
}
# count=0
# for(i in 1:dim(adj_matrix)[1]){
# for(j in 1: dim(adj_matrix)[2]){
# if(adj_matrix[i,j]==1){
# count=count+1
# }
#
# }}
g=graph.incidence(adj_matrix,weighted = NULL)
csv_graph=as.data.frame(adj_matrix)
col_sums=colSums(csv_graph)
row_sums=rowSums(csv_graph)
reduced_graph=adj_matrix[!(row_sums<10),]
reduced_graph2=adj_matrix[,!(col_sums<5)]
reduced_graph2=t(reduced_graph2)
#eliminate missing value
reduced_graph=reduced_graph[,!(colnames(reduced_graph)=="")]
reduced_graph2=reduced_graph2[,!(colnames(reduced_graph2)=="")]
write.csv(reduced_graph2,"actor_director_reduced_network.csv",col.names = TRUE,row.names = TRUE)
write.csv(reduced_graph,"director_actor_reduced_network.csv",col.names = TRUE,row.names = TRUE)
但是在我将其保存后,如果将其打开,则会在以下屏幕中看到以下内容:
然后:
network=read.csv("director_actor_reduced_network.csv", header=TRUE)
head(network)
> head(network)
X CCH.Pounder Johnny.Depp Christoph.Waltz Tom.Hardy Doug.Walker Daryl.Sabara J.K..Simmons Brad.Garrett Chris.Hemsworth
Alan.Rickman Henry.Cavill Kevin.Spacey Giancarlo.Giannini Peter.Dinklage Will.Smith Aidan.Turner Emma.Stone Mark.Addy Christopher.Lee
Naomi.Watts Leonardo.DiCaprio Robert.Downey.Jr. Liam.Neeson Bryce.Dallas.Howard Albert.Finney Hugh.Jackman Steve.Buscemi
Glenn.Morshower Bingbing.Li Tim.Holmes Jeff.Bridges Joe.Mantegna Ryan.Reynolds Tom.Hanks Christian.Bale Jason.Statham Peter.Capaldi
Jennifer.Lawrence Benedict.Cumberbatch Eddie.Marsan Jake.Gyllenhaal Charlie.Hunnam Harrison.Ford A.J..Buckley Kelly.Macdonald
Sofia.Boutella John.Ratzenberger Tzi.Ma Oliver.Platt Robin.Wright Channing.Tatum Jim.Broadbent Amy.Poehler ChloÃ..Grace.Moretz Jet.Li
Jimmy.Bennett Tom.Cruise Jeanne.Tripplehorn Joseph.Gordon.Levitt Scarlett.Johansson Angelina.Jolie.Pitt Gary.Oldman Tamsin.Egerton
Keanu.Reeves Jon.Hamm Judy.Greer Damon.Wayans.Jr. Jack.McBrayer Vivica.A..Fox Gerard.Butler Nick.Stahl Bradley.Cooper
Matthew.McConaughey Mark.Chinnery Paul.Walker Brad.Pitt Nicolas.Cage Justin.Timberlake Dominic.Cooper Bruce.Spence Jennifer.Garner
Zack.Ward Anthony.Hopkins Robert.Pattinson Janeane.Garofalo Bernie.Mac Robin.Williams Essie.Davis Josh.Gad Steve.Bastoni Kelli.Garner
Matthew.Broderick Seychelle.Gabriel Philip.Seymour.Hoffman Elisabeth.Harnois Ty.Burrell Jada.Pinkett.Smith Toby.Stephens
Ed.Begley.Jr. Bruce.Willis John.Michael.Higgins Sam.Shepard Matt.Frewer Kevin.Rankin Chris.Evans Colin.Salmon James.D.Arcy
Don.Johnson Mark.Rylance Matt.Damon Jim.Parsons Salma.Hayek Toby.Jones Daniel.Radcliffe Alfre.Woodard Rupert.Grint Miguel.Ferrer
Ronny.Cox Tony.Curran Jeremy.Renner Michael.Gough Clint.Howard Karen.Allen Suraj.Sharma Demi.Moo
我必须减少这个原因,因为它会很长。
> str(network)
'data.frame': 333 obs. of 6256 variables:
$ X : Factor w/ 333 levels "Étienne Faure",..: 120 238 51 22 150 193 271 79 122 108 ...
$ CCH.Pounder : int 1 0 0 0 0 0 0 0 0 0 ...
$ Johnny.Depp : int 0 0 0 0 0 0 0 0 0 0 ...
$ Christoph.Waltz : int 0 0 0 0 0 0 0 0 0 0 ...
$ Tom.Hardy : int 1 0 0 0 0 0 0 0 0 0 ...
$ Doug.Walker : int 0 0 0 0 0 0 0 0 0 0 ...
$ Daryl.Sabara : int 0 0 0 0 0 0 0 0 0 0 ...
$ J.K..Simmons : int 0 0 0 0 0 0 0 0 0 0 ...
$ Brad.Garrett : int 0 0 0 0 0 0 0 0 0 0 ...
$ Chris.Hemsworth : int 0 0 0 0 0 0 0 0 0 0 ...
$ Alan.Rickman : int 0 0 0 0 0 0 0 0 0 0 ...
$ Henry.Cavill : int 0 0 0 0 0 0 0 0 0 0 ...
$ Kevin.Spacey : int 0 0 0 0 0 0 0 0 0 0 ...
$ Giancarlo.Giannini : int 1 0 0 0 0 0 0 0 0 0 ...
$ Peter.Dinklage : int 0 0 0 0 0 0 0 0 0 0 ...
$ Will.Smith : int 0 0 0 0 0 0 0 0 0 0 ...
$ Aidan.Turner : int 1 0 0 0 0 0 0 0 0 0 ...
$ Emma.Stone : int 0 0 0 0 0 0 0 0 0 0 ...
$ Mark.Addy : int 0 0 1 0 0 0 0 0 0 0 ...
$ Christopher.Lee : int 0 1 0 0 0 0 0 0 1 0 ...
$ Naomi.Watts : int 0 0 0 0 0 0 0 0 0 0 ...
$ Leonardo.DiCaprio : int 0 0 0 0 0 1 0 0 0 0 ...
$ Robert.Downey.Jr. : int 0 0 0 0 0 0 0 0 0 0 ...
$ Liam.Neeson : int 1 0 0 0 0 0 0 0 0 0 ...
$ Bryce.Dallas.Howard : int 0 0 0 0 0 0 0 0 0 0 ...
$ Albert.Finney : int 0 0 0 0 0 0 0 0 0 0 ...
$ Hugh.Jackman : int 0 0 0 0 0 0 0 0 0 0 ...
$ Steve.Buscemi : int 1 0 0 0 0 0 0 0 0 0 ...
$ Glenn.Morshower : int 0 0 0 0 0 0 0 0 0 0 ...
$ Bingbing.Li : int 0 0 0 0 0 0 0 0 0 0 ...
$ Tim.Holmes : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jeff.Bridges : int 0 0 0 0 0 0 0 0 0 0 ...
$ Joe.Mantegna : int 0 0 0 0 0 0 0 0 0 0 ...
$ Ryan.Reynolds : int 0 0 0 0 0 0 0 0 0 0 ...
$ Tom.Hanks : int 0 0 0 0 0 0 0 0 0 0 ...
$ Christian.Bale : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jason.Statham : int 0 0 0 0 0 0 0 0 0 0 ...
$ Peter.Capaldi : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jennifer.Lawrence : int 0 0 0 0 0 0 0 0 0 0 ...
$ Benedict.Cumberbatch : int 0 0 0 0 0 0 0 0 0 0 ...
$ Eddie.Marsan : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jake.Gyllenhaal : int 0 0 0 0 0 0 0 0 0 0 ...
$ Charlie.Hunnam : int 1 0 0 0 0 0 0 0 0 0 ...
$ Harrison.Ford : int 0 0 0 0 0 0 0 0 0 0 ...
$ A.J..Buckley : int 0 0 0 0 0 0 0 0 0 0 ...
$ Kelly.Macdonald : int 0 0 0 0 0 0 0 0 0 0 ...
$ Sofia.Boutella : int 0 0 0 0 0 0 0 0 0 0 ...
$ John.Ratzenberger : int 0 0 1 0 0 0 0 0 0 0 ...
$ Tzi.Ma : int 0 0 0 0 0 0 0 0 0 0 ...
$ Oliver.Platt : int 0 0 0 0 0 0 0 0 0 0 ...
$ Robin.Wright : int 0 0 0 0 0 0 0 0 0 0 ...
$ Channing.Tatum : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jim.Broadbent : int 0 0 0 0 0 0 0 0 0 0 ...
$ Amy.Poehler : int 0 0 0 0 0 0 0 0 0 0 ...
$ ChloÃ..Grace.Moretz : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jet.Li : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jimmy.Bennett : int 0 0 0 0 0 0 0 0 0 0 ...
$ Tom.Cruise : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jeanne.Tripplehorn : int 0 0 0 0 0 0 0 0 0 0 ...
$ Joseph.Gordon.Levitt : int 0 0 0 0 0 0 0 0 0 0 ...
$ Scarlett.Johansson : int 0 0 0 0 0 0 0 0 0 0 ...
$ Angelina.Jolie.Pitt : int 0 0 0 0 0 0 0 0 0 0 ...
$ Gary.Oldman : int 0 0 0 0 0 0 0 0 0 0 ...
$ Tamsin.Egerton : int 0 0 0 0 0 0 0 0 0 0 ...
$ Keanu.Reeves : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jon.Hamm : int 0 0 0 0 0 0 0 0 0 0 ...
$ Judy.Greer : int 0 0 0 0 0 0 0 0 0 0 ...
$ Damon.Wayans.Jr. : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jack.McBrayer : int 0 0 0 0 0 0 0 0 0 0 ...
$ Vivica.A..Fox : int 0 0 0 0 0 0 0 0 0 0 ...
$ Gerard.Butler : int 0 0 0 0 0 0 0 0 0 0 ...
$ Nick.Stahl : int 0 0 0 0 0 0 0 0 0 0 ...
$ Bradley.Cooper : int 0 0 0 0 0 0 0 0 0 0 ...
$ Matthew.McConaughey : int 0 0 0 0 0 0 0 0 0 0 ...
$ Mark.Chinnery : int 0 0 0 0 0 0 0 0 0 0 ...
$ Paul.Walker : int 0 0 0 0 0 0 0 0 0 0 ...
$ Brad.Pitt : int 1 0 0 0 0 0 0 0 0 0 ...
$ Nicolas.Cage : int 0 0 1 0 0 0 0 0 0 0 ...
$ Justin.Timberlake : int 0 0 0 0 0 0 0 0 0 0 ...
$ Dominic.Cooper : int 1 0 0 0 0 0 0 0 0 0 ...
$ Bruce.Spence : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jennifer.Garner : int 0 0 0 0 0 0 0 0 0 0 ...
$ Zack.Ward : int 0 0 0 0 0 0 0 0 0 0 ...
$ Anthony.Hopkins : int 0 0 0 0 0 0 0 0 0 0 ...
$ Robert.Pattinson : int 0 0 0 0 0 0 0 0 0 0 ...
$ Janeane.Garofalo : int 0 0 0 0 0 0 0 0 0 0 ...
$ Bernie.Mac : int 0 0 0 0 0 0 0 0 0 0 ...
$ Robin.Williams : int 1 0 0 0 0 0 0 0 0 0 ...
$ Essie.Davis : int 0 0 0 0 0 0 0 0 0 0 ...
$ Josh.Gad : int 0 0 0 0 0 0 0 0 0 0 ...
$ Steve.Bastoni : int 0 0 0 0 0 0 0 0 0 0 ...
$ Kelli.Garner : int 0 0 0 0 0 0 0 0 0 0 ...
$ Matthew.Broderick : int 0 0 0 0 0 0 0 0 0 0 ...
$ Seychelle.Gabriel : int 0 0 0 0 0 0 0 0 0 0 ...
$ Philip.Seymour.Hoffman : int 0 0 0 0 0 0 0 0 0 0 ...
$ Elisabeth.Harnois : int 0 0 0 0 0 0 0 0 0 0 ...
$ Ty.Burrell : int 0 0 0 0 0 0 0 0 0 0 ...
$ Jada.Pinkett.Smith : int 0 0 0 0 0 0 0 0 0 0 ...
$ Toby.Stephens : int 0 0 0 0 0 0 0 0 0 0 ...
[list output truncated]
答案 0 :(得分:0)
您需要通过col.names=NA
。
从“ write.table”帮助文件中:
默认情况下,行名称的列没有列名。 如果“ col.names = NA”和“ row.names = TRUE”,则空白列名称为 添加了,这是读取CSV文件所使用的约定 电子表格。
您只需学习/记住R的那些怪癖之一。或者每次都抬头(好像我似乎必须这样做)。
=========编辑=======
您更新后的说明表明您的电子表格可能无法正常工作。确保文件以“ .csv”扩展名命名,并且电子表格程序使用逗号分隔单元格。
如果这不起作用,我们将需要对问题进行更好的描述。