r - 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!
解决方案
dfCCVul$logPopDens
是一个数据框,将其转换为向量。一种方法是使用unlist
.
dfCCVul$logPopDens <- unlist(dfCCVul$logPopDens)
或者我认为这也应该有效。
dfCCVul$logPopDens <- dfCCVul$logPopDens$PopDensity
推荐阅读
- java - Spring boot admin:客户端无法通过 https 向管理服务器注册
- swift - 如何居中水平图像列表SwiftUI
- docker - docker 中的所有 `npm install` 都已损坏
- laravel - 注册并登录谷歌
- r - 订购具有由“|”分隔的值的 csv 数据框 在 R?
- android - 由于 react-native-svg,iOS 无法构建
- css - 当硬编码值正常工作时,sass:color 失败
- xaml - 将通知标题和日期分开的好方法是什么?在设备的两个方向
- google-visualization - google可视化查询第二次迭代
- julia - 是什么让 Julia 可组合?