我用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 " }"
答案 0 :(得分:2)
在with
函数(cdata和temp_cdata)的末尾,您缺少括号
答案 1 :(得分:0)
您可以在修复cdata
和temp_data
括号问题后尝试这种方式。无需使用cdata
检查exists()
两次,请使用简单的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)
}
}