首页 > 解决方案 > Exporting R data - column classed as data.frame exports as blank with write_xlsx

问题描述

I am new to R, I looked for other solutions such as converting the datatype or exporting as csv (which generated weird formatting) and was unable to find a solution. I think I am overlooking something simple - thank you in advance!

I exported my dataframe (dfCCVul) to excel via write_xlsx. The data exported fine, except for the column "logPopDens.PopDensity" which is a column I had created by taking the log of another column (PopDensity) That column exports blank.

This is a snippet of the data :

PerPoverty  PerNotWhit  PerServWor  logPopDens.PopDensity
13.1        42.5        12.92       6.288305
30.2        48.9        13.03       4.861129
10.1        17.1        9.16        4.819233
26.3        49.8        23.32       4.862599
16.6        42.8        20.24       5.02263
12.5        25.6        8.28        4.448282
15.3        20.3        5.89        5.048188

When I check the data type of the new column, the results look embedded:

 $ logPopDens:'data.frame': 1315 obs. of  1 variable:
  ..$ PopDensity: num  3.52 3.07 2.64 1.16 2.27 ...

When I check the class, the output is:

> class(dfCCVul$logPopDens)
[1] "data.frame"

My thought was to convert the datatype, but I've received a series of errors after trying different syntax, for example:

> data$logPopDens <- as.numeric(as.character(data$logPopDens))
Error in data$col11 : object of type 'closure' is not subsettable 

> data$logPopDens.PopDensity <- as.numeric(as.character(data$logPopDens.PopDensity))
Error in data$logPopDens.PopDensity : 
  object of type 'closure' is not subsettable

Is there another way to export the values of the logPopDens?

Thank you!

标签: rdataframeexportxlsx

解决方案


dfCCVul$logPopDens是一个数据框,将其转换为向量。一种方法是使用unlist.

dfCCVul$logPopDens <- unlist(dfCCVul$logPopDens)

或者我认为这也应该有效。

dfCCVul$logPopDens <- dfCCVul$logPopDens$PopDensity

推荐阅读