转换R中的csv文件

时间:2017-12-07 07:36:02

标签: r csv

我在R.所以很新。你能告诉我如何将普通的行和列转换成数据帧吗?

例如,我有这样的数据:

Id, gender
1   1
2   0
3   1
4   0
5   0

但实际上这不是一个包含行和列的数据框,只是注意而不是作为一个表。

谢谢。

1 个答案:

答案 0 :(得分:0)

如果文件只是一个文本文件,只有标题行有逗号,则可以使用import tkinter as tk from PIL import Image, ImageTk # --- functions --- def eggvalue_change(): global eggvalue, eggvalueupgrade, money, eggz if money > int(round(eggvalueupgrade)): eggz -= eggvalueupgrade / eggvalue eggvalue+=0.2 eggvalueupgrade += int(round(eggvalueupgrade + (eggvalueupgrade / 7))) def moar_eggz(): global eggzps, chookz chookz += 1 def saving(): savedfile.write(money) savedfile.write(eggvalueupgrade) savedfile.write(eggzps) savedfile.write(chookz) savedfile.write(eggvalue) savedfile.write(eggz) def main_loop(): global eggz, eggzps, money eggzps = chookz / 100.0 money = eggz * eggvalue try: openbutton2.config( text="Egg Value: " + str(eggvalue) + " --> " + str(eggvalue+0.2) + " ($" + str(eggvalueupgrade) + ")", command=eggvalue_change) label2.config(text="Money: $" + str(int(round(money))), font=("Terminal", 50), borderwidth=5, relief="ridge", bg="black", foreground="white",pady=10, padx=498) if eggzps >= 10: label1.config(text="| Chickens: " + str(chookz)+" | Eggs: " + str(int(round(eggz)))+" | Eggs Per Second: " + str(int(round(eggzps)))+" | Egg Value: $" + str(eggvalue) + " |", font=("Terminal", 20), borderwidth=4, relief="ridge", bg="black", foreground="red",pady=10, padx=498) elif eggzps < 10: label1.config(text="| Chickens: " + str(chookz)+" | Eggs: " + str(int(round(eggz)))+" | Eggs Per Second: " + str(eggzps)+" | Egg Value: $" + str(eggvalue)+ " |", font=("Terminal", 20), borderwidth=4, relief="ridge", bg="black", foreground="red",pady=10, padx=498) if money < int(round(eggvalueupgrade)): openbutton2.config(fg="gray") elif money > int(round(eggvalueupgrade)): openbutton2.config(fg="white") except Exception as e: print(e) # display exception to see problem root.after(20, main_loop) def update_eggz(): global eggz try: eggz += eggzps except Exception as e: print(e) # display exception to see problem # repeat it after 1000ms root.after(1000, update_eggz) # --- main --- root = tk.Tk() root.title("Chicken Clicker") savedfile = open("savedata\\mainsave.txt", "a+") savedlines = savedfile.readlines() print savedlines if savedlines == []: eggz = 0 eggvalue = 0.2 chookz = 0 eggzps = 0.0 eggvalueupgrade = 100 money = eggz * eggvalue eggz = int(savedlines[0]) eggvalue = int(savedlines[1]) chookz = int(savedlines[2]) eggzps = int(savedlines[3]) eggvalueupgrade = int(savedlines[4]) money = int(savedlines[5]) savedfile.close() # empty labels - `update_labels` will add text label2 = tk.Label(root) label1 = tk.Label(root) label2.pack() label1.pack() chickencnv = Image.open("img\\1.png") chicken = ImageTk.PhotoImage(chickencnv) openbutton3 =tk.Button(root, bg="black", fg="white", font=("Terminal", 13), command=saving) openbutton2= tk.Button(root, bg="black", fg="gray", font=("Terminal", 15)) openbutton1= tk.Button(root, image=chicken, width=500, height=500, command=moar_eggz, bg="black") openbutton1.pack() openbutton2.pack() openbutton3.pack() # run it first time at once update_eggz() main_loop() root.mainloop() 读取该文件。使用read.table()skip=参数更正标题行。

col.names=

...和输出:

rawData <- "Id, gender
1   1
2   0
3   1
4   0
5   0"

aDataFrame <- read.table(textConnection(rawData),header=FALSE,col.names=c("Id","gender"),skip=1)

aDataFrame