问题描述
CSV文件如下所示(为简洁起见进行了修改).几列标题中都有空格,R似乎无法区分它们.
CSV file looks like this (modified for brevity). Several columns have spaces in their title and R can't seem to distinguish them.
Alias;Type;SerialNo;DateTime;Main status; [...]
E1;E-70;781733;01/04/2010 11:28;8; [...]
这是我要执行的代码:
s_data <- read.csv2( file=f_name )
attach(s_data)
s_df = data.frame(
scada_id=ID,
plant=PlantNo,
date=DateTime,
main_code=Main status,
seco_code=Additional Status,
main_text=MainStatustext,
seco_test=AddStatustext,
duration=Duration)
detach(s_data)
我也尝试过替代
main_code=Main\ status
和
main_code="Main status"
推荐答案
除非指定check.names=FALSE
,否则R会将不是有效变量名的列名(例如,包含空格或特殊字符或以数字开头)转换为有效变量名称,例如用点代替空格.尝试names(s_data)
.如果使用,请使用check.names=TRUE
,然后使用单引号(`)将名称引起来.
Unless you specify check.names=FALSE
, R will convert column names that are not valid variable names (e.g. contain spaces or special characters or start with numbers) into valid variable names, e.g. by replacing spaces with dots. Try names(s_data)
. If you do use check.names=TRUE
, then use single back-quotes (`) to surround the names.
我还建议使用reshape
软件包中的rename
.
I would also recommend using rename
from the reshape
package.
s_data <- read.csv2( file=f_name )
library(reshape)
s_df <- rename(s_data,ID="scada_id",
PlantNo="plant",DateTime="date",Main.status="main_code",
Additional.status="seco_code",MainStatustext="main_text",
AddStatustext="seco_test",Duration="duration")
对于价值而言,tidyverse工具(即readr::read_csv
)具有相反的默认值;除非您明确要求,否则它们不会转换列名称以使其成为合法的R符号.
For what it's worth, the tidyverse tools (i.e. readr::read_csv
) have the opposite default; they don't transform the column names to make them legal R symbols unless you explicitly request it.
这篇关于R:导入CSV的列名包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!