CSV Files पढ़ना
एक CSV file पढ़ना एक plain-text table, जैसे एक spreadsheet export, R में एक data frame की तरह load करता है। यह rows और columns की एक file को काम करने के लिए खोलने जैसा है।
In this page:
Syntax
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
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")
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- गलत working directory से
read.csv("people.csv")से एक file पढ़ना, इसलिए Rcannot open fileकहता है;getwd()check करें। read.csvसे एक semicolon-separated file पढ़ना, जबread.csv2याsep = ";"चाहिए।- 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: