← Back to R Course | Chapter 8: Data Manipulation | Lesson 1 of 7

CSV Files पढ़ना

एक CSV file पढ़ना एक plain-text table, जैसे एक spreadsheet export, R में एक data frame की तरह load करता है। यह rows और columns की एक file को काम करने के लिए खोलने जैसा है।
Syntax
r
df <- read.csv("file.csv")
df <- read.csv("file.csv", header = TRUE, stringsAsFactors = FALSE)

CSV Files पढ़ना

read.csv("file.csv") एक comma-separated file को सीधे एक data frame में पढ़ता है, data से automatically column types infer करते हुए। यह analysis के लिए R में tabular data load करने का सबसे common तरीका है।

header = TRUE (default) पहली row को column names की तरह treat करता है।

Note: अगर आप चाहते हैं text columns factors बनने की बजाय character रहें stringsAsFactors = FALSE pass करें (recent R versions में modern default)।

उदाहरण: Reading CSV Files

markup
csv_text <- "name,age\nAlice,30\nBob,25"
writeLines(csv_text, "people.csv")

df <- read.csv("people.csv")
print(df)
cat(class(df$age), "\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. गलत working directory से read.csv("people.csv") से एक file पढ़ना, इसलिए R cannot open file कहता है; getwd() check करें।
  2. read.csv से एक semicolon-separated file पढ़ना, जब read.csv2 या sep = ";" चाहिए।
  3. column में एक stray character की वजह से text की तरह stored numbers notice न करना, इसलिए class(df$age) character है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.