簡體   English   中英

讀取帶有名稱和標簽的.csv文件到R中

[英]read .csv file with Names and Labels into R

我有一個.csv文件,我需要讀入R.第一行包含名稱(例如BFI1,BFI2,CAQ2),第二行包含我也想在R中訪問的問題(例如“我喜歡參加派對”)。 前兩個之后的每一行對應一個參與者。

我希望能夠訪問R中的代碼和文本(例如,使用grep訪問一個調查中的所有問題,並且如果需要還可以查看項目文本。我需要數字響應為數字。

BFI1, BFI2, CAQ1, CAQ2
Likes to read, Enjoys Parties, Is Nervous, Loves Books
3, 7, 1, 4
4, 5, 3, 3

我想讀這個,以便我可以訪問名稱(第1行)或文本(可能是標簽)。 我看過Hmisc包,但它們的標簽功能似乎有限。

有沒有辦法讀取此.csv文件並訪問這兩個值?

不確定你是否可以將標簽作為單獨的矢量,但這是一個想法。 假設您的文件名是x.txt

## set up an argument list for scan() - just to avoid repetition
scanArgs <- list(
    file = "x.txt", what = "", nlines = 1, sep = ",", strip.white = TRUE
)

## read the data with no header and add the first line as names
df <- setNames(
    read.table("x.txt", skip = 2, sep = ","), 
    do.call(scan, scanArgs)
)
#   BFI1 BFI2 CAQ1 CAQ2
# 1    3    7    1    4
# 2    4    5    3    3

## make the label vector
labels <- setNames(do.call(scan, c(scanArgs, skip = 1)), names(df))
#            BFI1             BFI2             CAQ1             CAQ2 
# "Likes to read" "Enjoys Parties"     "Is Nervous"    "Loves Books" 

因此labels的元素對應於df的列,而列是數字。

請注意, x.txt是使用創建的

txt <- 'BFI1, BFI2, CAQ1, CAQ2
Likes to read, Enjoys Parties, Is Nervous, Loves Books
3,7,1,4
4,5,3,3'
writeLines(txt, "x.txt")

您可以使用nrows和skip參數或read.csv

nameFile <- "data.csv"

# read the first two lines
vectorNames <- read.csv(nameFile, nrows = 1)
vectorDescription <- read.csv(nameFile, nrows = 1, skip = 1)

# read the data
dfIn <- read.csv(nameFile, skip = 2)
names(dfIn) <- vectorNames

@Richard Scriven我使用了你的代碼並使用它來跟進它

library(Hmisc)
y=data.frame(temp=rep(NA,nrow(df)))  
for (i in 1:length(labels)){  
x=df[,i]  
label(x)=labels[i]   
y[names(df)[i]]=x  
}  
y$temp=NULL  
y  
#  BFI1 BFI2 CAQ1 CAQ2
# 1    3    7    1    4
# 2    4    5    3    3
label(y)
#            BFI1             BFI2             CAQ1             CAQ2 
# "Likes to read" "Enjoys Parties"     "Is Nervous"    "Loves Books" 

基於Michelle Usuelli的答案和Rich Scriven校正,您可以編寫此函數:

read_csv_with_labels <- function(fileName)
{
 library(Hmisc)

 # read the first two lines
 varNames <- read.csv(fileName, nrows = 1, stringsAsFactors = FALSE, header = FALSE)
 varLabels <- read.csv(fileName, nrows = 1, stringsAsFactors = FALSE, header = TRUE)

 # read the data
 df <- read.csv(fileName, skip = 2)

 # assign variable names and labels to the dataframe
 names(df) <- varNames
 label(df) <- varLabels 

 return(df)
}

我認為這應該包含在read.csv和read_csv的基本功能中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM