我的花括号放置有什么问题?
What's wrong with the placement of my curly braces?
我用 R 编写的程序在放置右大括号时遇到错误。错误出现在以下代码中:
file_list <- list.files()
for (file in file_list){
data <- read.fit(file)
# if the merged dataset doesn't exist, create it
if (!exists("cdata")){
cdata<-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp)
}
# if the merged dataset does exist, append to it
if (exists("cdata")){
temp_cdata <-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp)
cdata<-rbind(cdata, temp_cdata)
rm(temp_cdata)
}
}
我已经多次查看代码,没有发现任何错误原因,但我不断收到以下信息:
Error: unexpected '}' in:
" cdata<-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp)
}"
# if the merged dataset does exist, append to it
if (exists("cdata")){
temp_cdata <-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp)
cdata<-rbind(cdata, temp_cdata)
Error: unexpected symbol in:
" temp_cdata <-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp)
cdata"
rm(temp_cdata)
Warning message:
In rm(temp_cdata) : object 'temp_cdata' not found
}
Error: unexpected '}' in " }"
}
Error: unexpected '}' in " }"
您的两个 with
函数(cdata 和 temp_cdata)末尾都缺少括号
您可以在修复 cdata
和 temp_data
括号问题后尝试此方法。不需要用 exists()
检查 cdata
两次,使用简单的 if-else
file_list <- list.files()
for (file in file_list){
data <- read.fit(file)
# if the merged dataset doesn't exist, create it
if (!exists("cdata")){
cdata<-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp))
}else{
#append to it
temp_cdata <-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp))
cdata<-rbind(cdata, temp_cdata)
rm(temp_cdata)
}
}
我用 R 编写的程序在放置右大括号时遇到错误。错误出现在以下代码中:
file_list <- list.files()
for (file in file_list){
data <- read.fit(file)
# if the merged dataset doesn't exist, create it
if (!exists("cdata")){
cdata<-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp)
}
# if the merged dataset does exist, append to it
if (exists("cdata")){
temp_cdata <-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp)
cdata<-rbind(cdata, temp_cdata)
rm(temp_cdata)
}
}
我已经多次查看代码,没有发现任何错误原因,但我不断收到以下信息:
Error: unexpected '}' in:
" cdata<-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp)
}"
# if the merged dataset does exist, append to it
if (exists("cdata")){
temp_cdata <-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp)
cdata<-rbind(cdata, temp_cdata)
Error: unexpected symbol in:
" temp_cdata <-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp)
cdata"
rm(temp_cdata)
Warning message:
In rm(temp_cdata) : object 'temp_cdata' not found
}
Error: unexpected '}' in " }"
}
Error: unexpected '}' in " }"
您的两个 with
函数(cdata 和 temp_cdata)末尾都缺少括号
您可以在修复 cdata
和 temp_data
括号问题后尝试此方法。不需要用 exists()
检查 cdata
两次,使用简单的 if-else
file_list <- list.files()
for (file in file_list){
data <- read.fit(file)
# if the merged dataset doesn't exist, create it
if (!exists("cdata")){
cdata<-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp))
}else{
#append to it
temp_cdata <-with(data$record, data.frame (lat=position_lat, lon=position_long, speed=(speed/1000*60*60), alt=altitude, HR=heart_rate, time=timestamp))
cdata<-rbind(cdata, temp_cdata)
rm(temp_cdata)
}
}