标准化R中的原始数据 - 缺少值

时间:2014-05-03 20:55:27

标签: r function raw-data

我正在尝试将原始数据从文本文件转换为矩阵。我使用readLines()读取数据,然后将数据与grepl()分开(即男性; 20; 30.5 =>"男性"" 20&#34 ;" 30.5")到列表中。

唯一的问题是数据缺少一些未记录性别,年龄或体重或逗号代替小数点的值。在这些情况下,数据列表包含如下所示的行:

##"male"    "20"   "55.3"
##"male" "45" 

##"" "55" "55"

我想通过附加NA来应用函数来纠正这些实例。然后将该函数应用于lapply(data.dataList, function) R 中的功能不是我的最强点,但这是我的第一次尝试:

# function to correct column order for weight data
f.assignFields <- function(x) {
# create a blank character vector of length 3
out <- character(3)
sex <- grepl("[[:alpha:]]",x)
out[1] <- x[sex]
age.num <- which(as.numeric(x) <0)
out[2] <- ifelse(length(length(age.num) > 0, x[age.num], NA)
weight.num <- which(as.numeric(x) > 0)
out[3] <- ifelse(length(weight.num) > 0, x[weight.num], NA)
out
}

data.standardFields <- lapply(data.dataList, fassignFields)

我知道我想把带有字母的字符串放到第一列,然后将其他字符串放在第二列和第四列。我也应该更换&#34;,&#34;用&#34;。&#34;申请lapply()之前或之后的权重?只是在正确的方向上稍微推动将非常感激。

编辑:  从文本文件中提取的数据非常小。只有九个人记录他们的性别,年龄和体重。练习的目的是通过修改和转换数据来处理原始数据,以检查自己修改数据的有用性,而不是使用read.table()

male;28;81.3
male;45;
female; 17 ;57,2
female;64;62.8
male;16;55.3
male;;50,1
female;20.4;55
female;;
;55;55

这就是我的所作所为:

#read text file
weight.data <- readLines(text.txt)         

#removed white spaces
weight.data <- gsub(" ","",weight.data)
weight.data

[1] "male;28;81.3"     
[2] "male;45;"      
[3] "female;17;57,2"
[4] "female;64;62.8"  
[5] "male;16;55.3"   
[6] "male;;50,1"       
[7] "female;20.4;55"     
[8] "female;;"          
[9] ";55;55" 

#split strings by semicolon
weight.dataList <-strsplit(weight.data, split = ";")
weight.dataList

[[1]]
[1] "male"    "28"   "81.3"

[[2]]
[1] "male" "45"  

[[3]]
[1] "female" "17"     "57,2"  

[[4]]
[1] "female" "64"   "62.8"

[[5]]
[1] "male"  "16"   "55.3"

[[6]]
[1] "male"    ""     "50,1"

[[7]]
[1] "female"    "20.4" "55"  

[[8]]
[1] "female" ""  

[[9]]
[1] ""   "55" "55"

我想将NA&#39添加到缺失的行中。我正在尝试创建一个函数来纠正字段的行dimnensions。例如,第二个条目的重量应该是NA。

# function to correct column order and size for weight data
f.assignFields <- function(x) {
# create a blank character vector of length 3
out <- character(3)
sex <- grepl("[[:alpha:]]",x)
# puts sex in first column
out[1] <- x[sex]
# assigns NA if age missing
age.num <- which(as.numeric(x) <0)
out[2] <- ifelse(length(length(age.num) > 0, x[age.num], NA)
# assigns NA if weight missing
weight.num <- which(as.numeric(x) > 0)
out[3] <- ifelse(length(weight.num) > 0, x[weight.num], NA)
out
}

data.standardFields <- lapply(data.dataList, fassignFields)

最后,我将使用unlist()matrix()将数据转换为行列格式。我想用NA替换数据的缺失值,按以下顺序排列数据&#34;性别,年龄,体重&#34;并固定重量,使55,1显示为55.1。

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用read.table,但似乎你的教授试图折磨你。任何地方都没有任何数据可以将20.4列为一个人的年龄。

> ## txt <- "male;28;81.3
  ## male;45;
  ## female; 17 ;57,2
  ## female;64;62.8
  ## male;16;55.3
  ## male;;50,1
  ## female;20.4;55
  ## female;;
  ## ;55;55"
> x <- gsub("\\s+", "", readLines(textConnection(txt))) 
> rpl.comma <- gsub(",", ".", x)
> spl <- strsplit(rpl.comma, ";")
> M <- matrix(0, nrow = length(x), ncol = 3)
> for(j in 1:3){
    M[,j] <- sapply(seq(spl), function(i){
      ifelse(spl[[i]][j] == "", "NA", spl[[i]][j])
    })
  }
> DF <- data.frame(M)
> names(DF) <- c("sex", "age", "weight")
> DF
##      sex  age weight
## 1   male   28   81.3
## 2   male   45   <NA>
## 3 female   17   57.2
## 4 female   64   62.8
## 5   male   16   55.3
## 6   male   NA   50.1
## 7 female 20.4     55
## 8 female   NA   <NA>
## 9     NA   55     55