所以我编写了一个代码,允许我在单个列中存储值。但是,我遇到的问题是我的数据包含在多个列中,所有列都有不同的长度,并且其中包含NA。我用于分箱的脚本如下: -
bin <- seq(min(data[, 1]), max(data[, 1]), by = 0.0005)
binnedData <- tapply(data[, 1], cut(data[, 1], breaks = bin), median)
我想知道是否有一种方法可以将所有列中的所有值组合成一个巨大的长列,所以我基本上可以运行它,或者是否有一种方法我可以调整它以便我可以在我的1000 x 1000矩阵上运行它
这取自部分数据: -
102.23144 123.23242 102.23145
103.23144 123.23242 102.36563
103.83637 NA 102.36356
104.23225 NA 102.23423
105.87890 NA NA
只有组合列值的预期结果: -
102.23144
103.23144
103.83637
104.23225
105.87890
123.23242
123.23242
102.23145
102.36563
102.36356
102.23423
结合binning的结果: -
(102.0000 - 102.0005) - Median of all values that fall into bin
(102.0005 - 102.0010) - Median of all values that fall into bin
(102.0015 - 102.0020) - Median of all values that fall into bin
由于
答案 0 :(得分:1)
您是否尝试过使用reshape2包中的融合功能。
以下是您的部分测试数据:
test <- data.frame(V1=c(102.2314,103.2314,103.8364,104.2322,105.8789),
V2=c(123.2324,123.2324,NA,NA,NA),
V3=c(102.2314,102.3656,102.3636,102.2342,NA)
)
> test
V1 V2 V3
1 102.2314 123.2324 102.2314
2 103.2314 123.2324 102.3656
3 103.8364 NA 102.3636
4 104.2322 NA 102.2342
5 105.8789 NA NA
然后使用融合功能 -
test_m <- melt(test)
但是有NAs。
> test_m
variable value
1 V1 102.2314
2 V1 103.2314
3 V1 103.8364
4 V1 104.2322
5 V1 105.8789
6 V2 123.2324
7 V2 123.2324
8 V2 NA
9 V2 NA
10 V2 NA
11 V3 102.2314
12 V3 102.3656
13 V3 102.3636
14 V3 102.2342
15 V3 NA
所以,现在与此过滤器同步 -
test_m<- melt(test)[which(!(is.na(melt(test)[,2]))),]
> test_m
variable value
1 V1 102.2314
2 V1 103.2314
3 V1 103.8364
4 V1 104.2322
5 V1 105.8789
6 V2 123.2324
7 V2 123.2324
11 V3 102.2314
12 V3 102.3656
13 V3 102.3636
14 V3 102.2342
所以,NAs被删除了。您只能选择数据的第二列删除变量名称col。
答案 1 :(得分:1)
如果您有data.frame
,可以使用stack
base R
来完成此操作
na.omit(stack(test))[,1,drop=FALSE]
# values
#1 102.2314
#2 103.2314
#3 103.8364
#4 104.2322
#5 105.8789
#6 123.2324
#7 123.2324
#11 102.2314
#12 102.3656
#13 102.3636
#14 102.2342
或者
data.frame(V1= unname(na.omit(unlist(test))))
test <- structure(list(V1 = c(102.2314, 103.2314, 103.8364, 104.2322,
105.8789), V2 = c(123.2324, 123.2324, NA, NA, NA), V3 = c(102.2314,
102.3656, 102.3636, 102.2342, NA)), .Names = c("V1", "V2", "V3"
), class = "data.frame", row.names = c("1", "2", "3", "4", "5"))