我正在逐一阅读第二栏的表格。 这是我的R代码。 x的输出是字符串,它无法检索列值。我尝试使用'as.factor()'和'gsub()',但没有成功。
# Read the table .
filename = read.table("table.txt", row.names=1, sep="\t", header=TRUE)
# Assigning to dataframe .
filename <- data.frame(filename)
# Getting the column names .
column_name <- colnames(filename)
print (column_name)
# Print the name of 2nd column onwards up to end .
for (i in 2:length(column_name)){
print (column_name[i]);
## Here , I want to parse the filename with column name ?
## For example table has columns "MONTH", "Pressure", "Temp".
## O/P: filename$Pressure ,filenameT$emp
############################################
x <- paste("filename$",column_name[i],sep="");
############################################
print (x );
names ( x ) <- rownames ( filename );
}
例如:
Month Pressure Temp Wind
JAN 17 36 120
FEB 10 34 110
MAR 13 30 115
APR 14 33 105
.......
O / P:
17
10
13
14
36
34
30
33
120
110
115
105
答案 0 :(得分:0)
x <- eval(parse(text=paste("filename","$",column_name[i],sep="")))
试试这个......也许它会起作用。