r - Object not found - R
问题描述
I am trying to construct data.tree from json/dataframe I have got.
library(data.tree)
construct_tree <- function(x) {
gq <- Node$new("sessions")
for(i in 1:nrow(x)) {
if(x[i,c("type")] != 'RECORD')
gbq$AddChild(x[i,c("name")])
else
y <- as.data.frame(x[i,c('fields')])
print(y)
}
gq
}
This is how the data looks,
dput(data_samp)
structure(list(name = c("date", "totals"), type = c("STRING",
"RECORD"), fields = list(NULL, structure(list(mode = c("NULLABLE",
"NULLABLE", "NULLABLE", "NULLABLE", "NULLABLE", "NULLABLE", "NULLABLE",
"NULLABLE", "NULLABLE", "NULLABLE", "NULLABLE", "NULLABLE", "NULLABLE"
), name = c("visits", "hits", "pageviews", "timeOnSite", "bounces",
"transactions", "transactionRevenue", "newVisits", "screenviews",
"uniqueScreenviews", "timeOnScreen", "totalTransactionRevenue",
"sessionQualityDim"), type = c("INTEGER", "INTEGER", "INTEGER",
"INTEGER", "INTEGER", "INTEGER", "INTEGER", "INTEGER", "INTEGER",
"INTEGER", "INTEGER", "INTEGER", "INTEGER")), .Names = c("mode",
"name", "type"), class = "data.frame", row.names = c(NA, 13L)))), class = "data.frame", row.names = c(NA,
-2L), .Names = c("name", "type", "fields"))
But in the else condition, when i try to use object 'y', i keep getting Error in print(y) : object 'y' not found
Can anyone advise what i am doing wrong here. Thanks.
解决方案
You need an extra curly braces around the else condition statements:
library(data.tree)
construct_tree <- function(x) {
gq <- Node$new("sessions")
for(i in 1:nrow(x)) {
if(x[i,c("type")] != 'RECORD')
gbq$AddChild(x[i,c("name")])
else{
y <- as.data.frame(x[i,c('fields')])
print(y)}
}
gq
}
推荐阅读
- windows - 已连接到 AWS AMI 实例,但我现在如何使用 Jupyter 笔记本?
- javascript - 无法将 HTML 表格单元格中的输入数据保存到 JavaScript 变量中
- zeromq - 用于接收和发送消息的 pyzmq 轮询器
- c++ - 为什么 std::derived_from 概念是通过添加 cv 限定符的附加可转换性测试来实现的?
- google-api - 为什么午夜配额重置后第一个也是唯一一个 GMail API 请求失败?
- android - Cloud Firestore 数据未加载
- sql - 基于计费周期的用户年度总支出 (SQL)
- typescript - 将多个 vscode 调试配置统一为一个
- ios - Flutter 在为 iOS 构建时卡在“正在运行 Xcode 构建...”
- java - 在 Spring Security 中如何实现 SAML 和自定义身份验证处理程序