我有一个CSV文件,大约有800列,不同的编号。数据和我正在计算每列的幅度和一些其他参数。但是当我在csv文件中写入数据时,它会附加在一个列中,我希望将数据列明智地附加到另一个文件中。
假设:以下是数据。
col_1 col_2 col_3 col_4 col_5
1.1612 0.3067 -1.1993 0.2182 -0.9918
2.0096 -0.0519 -1.0422 0.0748 0.0259
1.6678 1.1963 -0.1755 0.0198 -0.2106
0.4608 0.676 -0.7721 0.6531 0.5783
0.5463 0.7111 -0.7782 0.4303 -0.3082
-0.1022 0.9064 -0.6317 0.5188 -1.2695
0.4211 0.5859 0.1846 0.1434 -0.4181
1.2848 -0.5249 1.1307 -0.5768 0.0336
0.7813 0.6989 -0.0885 -0.5295
0.4288 1.0056 0.2213
0.8469 0.9048
脚本:
i=1
for(i in 1:5){
readfile <- read.csv("test.CSV" , header = FALSE, skip = 1)[,i]
readfile
colnm <- na.omit(readfile)
colnm
arr_colmn <- array(colnm)
arr_colmn
no._of_rows <- nrow(arr_colmn) ### For single column row count
no._of_rows
##other calculation not mentioned
AMP <- abs(fft(arr_colmn,inverse = FALSE))
YY <- data.frame(AMP)
write.table(YY, "test_1.csv", append = TRUE,col.names = TRUE, row.names = FALSE, sep = ",")
i=i+1
}`
Col_1_Result col_2_Result Col_3_Result............
8.3432 3.000 0.611
17.2494 14.42 0.8412
7.72650 15.76
. .
. .
. .
. .
答案 0 :(得分:0)
我真的不明白你如何生成输出表,你提供的代码似乎没有重现表;不幸的是,你的其他评论也没有真正帮助。
除此之外,我认为你想为不同长度的列计算某种形式的汇总统计数据。
在以下示例中,我为每列计算mean
和sd
,并将结果存储在dataframe
中。请注意,for
的每一列都不需要lapply
循环,只需dataframe
个函数。
# Your sample data
data <- structure(list(V1 = c(1.1612, 2.0096, 1.6678, 0.4608, 0.5463,
-0.1022, 0.4211, 1.2848, 0.7813, 0.4288, -0.3342, 0.2777, 0.8392,
-0.5539, -0.8286, -0.9506, -1.1383, -1.7395, -0.7492), V2 = c(0.3067,
-0.0519, 1.1963, 0.676, 0.7111, 0.9064, 0.5859, -0.5249, 0.6989,
1.0056, 0.8469, 1.1276, 2.3087, -0.8514, -1.1398, NA, NA, NA,
NA), V3 = c(-1.1993, -1.0422, -0.1755, -0.7721, -0.7782, -0.6317,
0.1846, 1.1307, -0.5127, -0.6622, 0.3677, 0.2151, -0.0458, -1.7883,
-0.9445, 0.5524, -0.9857, -0.9644, -1.6342), V4 = c(0.2182, 0.0748,
0.0198, 0.6531, 0.4303, 0.5188, 0.1434, -0.5768, -0.0885, 0.2213,
0.9048, -0.0305, -1.3046, -0.4242, 0.1053, 0.9766, -0.5783, 0.7462,
NA), V5 = c(-0.9918, 0.0259, -0.2106, 0.5783, -0.3082, -1.2695,
-0.4181, 0.0336, -0.5295, -0.415, 1.2634, 0.769, -0.1862, -0.7126,
-0.2716, -0.3479, 1.1002, NA, NA)), .Names = c("V1", "V2", "V3",
"V4", "V5"), class = "data.frame", row.names = c(NA, -19L))
# Calculate mean and sd for every column; ignore NA's
df <- do.call(rbind.data.frame, lapply(d, function(x)
c(mean(x, na.rm = TRUE), sd(x, na.rm = TRUE))));
colnames(df) <- c("mean", "sd");
df;
# mean sd
#1 0.1832684 1.0095877
#2 0.5201400 0.8748568
#3 -0.5098053 0.7594946
#4 0.1116500 0.5779139
#5 -0.1112118 0.6902565
# Write to file
write.csv(df, file = "output.csv", row.names = FALSE, quote = FALSE);
答案 1 :(得分:0)
使用此方法,您可以从列中读取列数据,并将数据附加到新CSV的列中。
readfile <- read.csv("DATA.csv" , header = TRUE, check.names = FALSE) # Read the file
R <- nrow(readfile) ##No. of Total Rows
C <- ncol(readfile) ##No. of Columns
datalist <- NULL # CReating a Null Dataframe
i=1
for (i in 1:C) {
l <-na.omit(readfile[[i]]) # NO. of Rows Of ith Column
Amp <- abs((array(l)) ###Any calculation: i have taken ABS
combine_df <- data.frame(Amp)
datalist[[i]] <- combine_df ## Adding data to NULL df
i=i+1
}
Data_Final_1 = do.call(data.frame,datalist) ###Extracting the final Data
write.csv(Data_Final, "DATA_1.csv",row.names = FALSE, na = "")