我是R.的新手。我正在尝试使用" write.csv"命令在R中写入一个csv文件。不幸的是,当我这样做时,结果数据框产生的字符串前缀为X,尽管该文件已经有一个列名。
它生成X_name1,X_name2
请告诉我你的建议
我添加了一个类似于我的数据的示例代码。
a<- c("1","2")
b <- c("3","4")
df <- rbind(a,b)
df <- as.data.frame(df)
names(df) <- c("11_a","12_b")
write.csv(df,"mydf.csv")
a <- read.csv("mydf.csv")
a
#Result
X X11_a X12_b
1 a 1 2
2 b 3 4
我所需要的只是&#34; 11_a&#34;和&#34; 12_b&#34;作为列名。但它也包含前缀X.
答案 0 :(得分:4)
在重新读取数据时使用check.names=FALSE
- 在R中通常不接受以数字开头的名称:
read.csv(text="11_a,12_b
a,1,2
b,3,4", check.names=FALSE)
# 11_a 12_b
#a 1 2
#b 3 4
read.csv(text="11_a,12_b
a,1,2
b,3,4", check.names=TRUE)
# X11_a X12_b
#a 1 2
#b 3 4
答案 1 :(得分:0)
当您阅读.csv文件时,您所要做的就是将public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String typeName = s.next();
//ask for type
Class<?> type;
try {
type = Class.forName(typeName);
} catch (ClassNotFoundException e) {
System.out.printf("Class `%s` was not found%n", typeName);
return;
}
//discover constructors
Constructor<?>[] constructors = type.getConstructors();
for (int i = 0; i < constructors.length; ++i) {
System.out.printf(" > %d %s%n", i, constructors[i]);
}
//select constructor and list its parameters
int constructorIndex = s.nextInt();
Constructor<?> constructor = constructors[constructorIndex];
Type[] parameterTypes = constructor.getGenericParameterTypes();
for (Type parameterType : parameterTypes) {
System.out.println(parameterType);
//each type implementation requires a specific processing
if (parameterType instanceof Class) {
Class parameterClass = (Class) parameterType;
if ((parameterClass).isPrimitive()) {
//simple int primitive; which can be directly obtained from scanner
if (Integer.TYPE.isAssignableFrom(parameterClass)) {
System.out.println("\tinteger primitive > ");
int value = s.nextInt();
System.out.println("\t you've entered " + value);
}
} else {
Stream.of((parameterClass).getConstructors()).forEach(c -> System.out.printf("\t%s%n", c));
Stream.of((parameterClass).getDeclaredConstructors()).forEach(c -> System.out.printf("\t%s%n", c));
}
}
}
// here we consider a sample java.lang.Integer class was requested with #0 constructor
if (Integer.class.isAssignableFrom(type) && constructorIndex == 0) {
System.out.println("Input an integer number: ");
String value = s.next();
// since newInstance requires array of objects, we need to create an object (not primitive)
Object instance;
try {
instance = constructor.newInstance(Integer.valueOf(value));
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
return;
}
System.out.printf("%s %s%n", instance.getClass(), instance);
}
}
添加到您的代码中。它看起来像是:
header=TRUE