以“1232,12”作为编号导入csv的最佳方法

时间:2017-09-03 07:10:12

标签: r csv import

我想导入具有此配置的csv

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.*;
import java.io.*;

public class reading_a_file {

static void pull_info(String courseCode) throws IOException{

    BufferedReader read = new BufferedReader( new FileReader("CourseSample.txt"));

     Map<String, String> course_info = new HashMap<String, String>();

     String line;
    String[] data;

    while(((line = read.readLine()) != null))
   {
       line = read.readLine();
       data = line.split(" ");
       String courseID = data[0];
       String courseName = data[1];

       course_info.put(courseID, courseName);
       if(courseCode==courseID){
    System.out.println(course_info.get(courseName));
        }
    }

}

public static void main(String[] args) throws IOException{
    Scanner console = new Scanner (System.in);
    String course_code;

    System.out.println("--------------------------------------------------");
    System.out.println("Welcome to text file formatter by XM21");
    System.out.println("--------------------------------------------------");
    System.out.println("");
    System.out.println("Please enter course id to see description (-1 to exit):");
    System.out.println("CourseCode:");
    course_code = console.nextLine();


    pull_info(course_code);

}
}

我试图使用fread(来自library data.table)或read.table导入它,但c2始终被用作字符列。我知道我可以将它转换为数字,但我期待的是将其直接导入为数字data.colum

我试过的是:

c1,c2,c3
1,"32,12",A
2,"23,43",B

但两者都不能像我想要的那样工作

提前致谢

1 个答案:

答案 0 :(得分:2)

使用read.csv2。显然,它会在第一次尝试时读取您的数据。

dat <- read.csv2(text = '
c1,c2,c3
1,"32,12",A
2,"23,43",B
', sep = ",", quote = '"')

dat
  c1    c2 c3
1  1 32.12  A
2  2 23.43  B

P.S。 data已经是基本R函数的名称,使用其他东西会更好。