从R中的csv文件读取数据 [英] Reading the data from a csv file in R
本文介绍了从R中的csv文件读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从R中的csv文件中读取一些数据以计算t检验函数,但是我没有成功做到这一点,并且根据调试器,参数不是数字或逻辑的:返回NA.
I want to read some data from a csv file in R in order to compute a t-test function but I didn't success to do that and accoriding to the debugger, the arguments are not numeric or logical: returning NA.
这是t检验的代码,无需读取csv文件:
This is the code of the t-test without reading from the csv file:
labels <- list('non-failing heart (NF)', 'failing heart (F)')
data <- list(c(99, 52), c(96, 40), c(100, 38), c(105, 18),
c(NA_integer_, 11), c(NA_integer_, 5), c(NA_integer_, 42),
c(NA_integer_, 55), c(NA_integer_, 53), c(NA_integer_, 39),
c(NA_integer_, 42), c(NA_integer_, 50))
df <- setNames(do.call(rbind.data.frame,
lapply(data, function(d) data.frame(d[1], d[2]))),
labels)
results <- t.test(df[['non-failing heart (NF)']], df[['failing heart (F)']])
results$statistic
results$estimate
results$p.value
这就是我尝试过的:
labels <- list('non-failing heart (NF)', 'failing heart (F)')
data <- read.csv2(file="C:\\Users\\alex\\Downloads\\data.csv", sep=";", dec=".", header=TRUE)
df <- setNames(do.call(rbind.data.frame,
lapply(data, function(d) data.frame(d[1], d[2]))),
labels)
results <- t.test(df[['non-failing heart (NF)']], df[['failing heart (F)']])
results$statistic
results$estimate
results$p.value
这是我的文件"data.csv"的处理方式看起来像:
This is how my file "data.csv" looks like:
然后将使用以下代码在Boxplot中将其可视化:
Then it will be visualized in Boxplot using this code:
df$NF <- df$`non-failing heart (NF)`
df$F <- df$`failing heart (F)`
boxplot(df[3:4],
data=df,
cex.lab=0.65,
xlab="Fig. 1 IRE binding activity for non-failing (NF) and failing (F) hearts.",
ylab="IRE binding activity (%)",
col="orange",
border="brown",
ylim = c(0, 120)
)
推荐答案
您将'NA_integer _'
作为字符串.将它们转换为实际的 NA
值,然后执行 t.test
.
You have 'NA_integer_'
as a string. Turn them to actual NA
values and then perform t.test
.
data[data == 'NA_integer_'] <- NA
t.test(data$NF, data$F)
这篇关于从R中的csv文件读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文