R代码没有创建对象?

时间:2016-03-29 22:17:42

标签: r

我为大学任务写了一些代码。赋值基于各种具体样品及其拉伸强度。有20种混凝土混合物(由四种不同的促进剂和五种不同的增塑剂制成)。我们的工作是对这个数据框进行统计分析:

TStrength accelerator plasticiser
1   3.417543           1           1
2   2.887113           1           2
3   3.600988           1           3
4   3.702631           1           4
5   3.686944           1           5
6   3.699785           1           1
7   3.112972           1           2
8   3.918160           1           3
9   3.600538           1           4
10  2.748832           1           5
11  3.404498           1           1
12  3.735437           1           2
13  3.347577           1           3
14  3.101556           1           4
15  3.527621           1           5
16  3.856831           1           1
17  3.492118           1           2
18  3.928343           1           3
19  3.511689           1           4
20  3.371985           1           5
21  3.069794           2           1
22  3.168010           2           2
23  3.316657           2           3
24  3.455162           2           4
25  2.818250           2           5
26  4.054507           2           1
27  3.065984           2           2
28  3.201351           2           3
29  3.417554           2           4
30  3.364320           2           5
31  3.218677           2           1
32  2.647151           2           2
33  3.222705           2           3
34  3.145210           2           4
35  3.636642           2           5
36  3.317620           2           1
37  3.645922           2           2
38  2.556071           2           3
39  3.177663           2           4
40  3.014374           2           5
41  3.838183           3           1
42  4.155951           3           2
43  3.886330           3           3
44  3.723898           3           4
45  4.425442           3           5
46  3.738460           3           1
47  3.217834           3           2
48  3.942241           3           3
49  3.699851           3           4
50  3.797089           3           5
51  3.652456           3           1
52  4.851609           3           2
53  3.359099           3           3
54  4.089559           3           4
55  4.282991           3           5
56  3.803784           3           1
57  3.519551           3           2
58  3.935084           3           3
59  3.890324           3           4
60  4.611936           3           5
61  3.343098           4           1
62  3.713952           4           2
63  3.629883           4           3
64  3.082509           4           4
65  3.346548           4           5
66  3.277845           4           1
67  3.509506           4           2
68  3.490567           4           3
69  3.235009           4           4
70  3.970925           4           5
71  3.504646           4           1
72  3.270798           4           2
73  3.547298           4           3
74  3.278489           4           4
75  3.322743           4           5
76  2.975010           4           1
77  3.384996           4           2
78  3.399486           4           3
79  3.703567           4           4
80  3.214973           4           5

我的第一步是尝试找出20种混凝土类型中每种类型的Tstrength值的均值(每种独特的混凝土样本有四种类型)。我是R的新手,我的代码肯定不漂亮,但这是我写的代码找到的代码:

#Setting the correct directory
setwd("C:/Users/Matthew/Desktop/Work/Engineering")

#Creating the data frame object, Concrete.
#Note that this will only work if the file
#s...-CW.dat is in the current working directory
#Therefore for this code to work, CreateData.r must
#be run on the individual computer with the
#given matriculation number, and the file must be saved
#in the specified directory

Concrete<-read.table(file='s...-CW.dat',header=TRUE)

#Since the samples of concrete are made from 4 different accelerators and
#5 different plasticisers there will be 4*5=20 unique combinations from
#which concrete samples can come from (i.e. 1,1; 1,2; 4,5 etc).
# There are four samples of each combination


#The next section of code is used to find the mean of the four samples,
#for each combination (20 total)


#creating a list with Tstrength from all (1,1) combinations
#Then finding average
combo1 = list(Concrete[1,1],Concrete[6,1],Concrete[11,1],Concrete[16,1])
combo1mean = mean(unlist(combo1))

#Repeating for (1,2)
combo2 = list(Concrete[2,1],Concrete[7,1],Concrete[12,1],Concrete[17,1])
combo2mean = mean(unlist(combo2))

#Repeating for (1,3)
combo3 = list(Concrete[3,1],Concrete[8,1],Concrete[13,1],Concrete[18,1])
combo3mean = mean(unlist(combo3))

#Repeating for (1,4)
combo4 = list(Concrete[4,1],Concrete[9,1],Concrete[14,1],Concrete[19,1])
combo4mean = mean(unlist(combo4))

#Repeating for (1,5)
combo5 = list(Concrete[5,1],Concrete[10,1],Concrete[15,1],Concrete[20,1])
combo5mean = mean(unlist(combo5))

#Repeating for (2,1)
combo6 = list(Concrete[21,1],Concrete[26,1],Concrete[31,1],Concrete[36,1])
combo6mean = mean(unlist(combo6))

#Repeating for (2,2)
combo7 = list(Concrete[22,1],Concrete[27,1],Concrete[32,1],Concrete[37,1])
combo7mean = mean(unlist(combo7))

#Repeating for (2,3)
combo8 = list(Concrete[23,1],Concrete[28,1],Concrete[33,1],Concrete[38,1])
combo8mean = mean(unlist(combo8))

#Repeating for (2,4)
combo9 = list(Concrete[24,1],Concrete[29,1],Concrete[34,1],Concrete[39,1])
combo9mean = mean(unlist(combo9))


#Repeating for (2,5)
combo10 = list(Concrete[25,1],Concrete[30,1],Concrete[35,1],Concrete[40,1])
combo10mean = mean(unlist(combo10))


#Repeating for (3,1)
combo11 = list(Concrete[41,1],Concrete[46,1],Concrete[51,1],Concrete[56,1])
combo11mean = mean(unlist(combo11))


#Repeating for (3,2)
combo12 = list(Concrete[42,1],Concrete[47,1],Concrete[52,1],Concrete[57,1])
combo12mean = mean(unlist(combo12))

#Repeating for (3,3)
combo13 = list(Concrete[43,1],Concrete[48,1],Concrete[53,1],Concrete[58,1])
combo13mean = mean(unlist(combo13))


#Repeating for (3,4)
combo14 = list(Concrete[44,1],Concrete[49,1],Concrete[54,1],Concrete[59,1])
combo14mean = mean(unlist(combo14))


#Repeating for (3,5)
combo15 = list(Concrete[45,1],Concrete[50,1],Concrete[55,1],Concrete[60,1])
combo15mean = mean(unlist(combo15))

#Repeating for (4,1)
combo16 = list(Concrete[61,1],Concrete[66,1],Concrete[71,1],Concrete[76,1])
combo16mean = mean(unlist(combo16))

#Repeating for (4,2)
combo17 = list(Concrete[62,1],Concrete[67,1],Concrete[72,1],Concrete[77,1])
combo17mean = mean(unlist(combo17))

#Repeating for (4,3)
combo18 = list(Concrete[63,1],Concrete[68,1],Concrete[73,1],Concrete[78,1])
combo18mean = mean(unlist(combo18))

#Repeating for (4,4)
combo19 = list(Concrete[64,1],Concrete[69,1],Concrete[74,1],Concrete[79,1])
combo19mean = mean(unlist(combo19))

#Repeating for (4,5)
combo20 = list(Concrete[65,1],Concrete[70,1],Concrete[75,1],Concrete[80,1])
combo20mean = mean(unlist(combo20))

关于代码的一些注释:&#34; s ...&#34;只是我的预科号码。我有三重检查,我没有在文件名或存储位置的目录中犯了错误。 CreataData.r只是一个提供给我们的脚本,用于生成用于创建“混凝土”的数据。根据我们的预科编号(所以我们不仅仅是盲目地互相抄袭)。

我对代码的问题是,无论何时运行,都会创建对象Concrete,combo1mean,combo2mean和combo3mean也是如此。但是,我无法弄清楚为什么其他物体没有被创造出来。

我在Rgui中运行脚本没有成功。运行脚本后,它告诉我检查Concrete是否已初始化,然后检查combo4mean及以上是否已初始化,但他们从未这样做过。我认为它可能与运行错误的文件有关,或者我没有正确保存数据,但是脚本肯定包含所有代码,我创建了一个新文件以查看是否可行,但不幸的是它没有。另外,我读过W.N.Venables,D.M。的R介绍。史密斯和R核心团队,但没有任何帮助我解决这个问题。

PS我不是这样做的一个简单的家庭作业方式。我真的试图弄清楚出了什么问题,但我似乎无法找到问题所在。如果问题不准确,或者如果我有误解,我也很抱歉,我对R很新,我正在尽我所能去学习它!提前干杯。

编辑:以防万一有人好奇,我设法从一个空的工作区开始,获得完全相同的代码在另一台计算机上工作。我还不太清楚为什么它在第一台计算机上没有工作,但感谢42代码建议。

1 个答案:

答案 0 :(得分:2)

添加应绕过与阅读文本文件相关的问题的代码。这可以在任何R安装上成功:

public boolean isDiagonal(int row,int col){
    if (row == board.length-1 && board[row-1][col+1] == 'T') {
        return true;
    } else if (col == board.length -1 && board[row+1][col-1] == 'T') {
        return true;
    } else if (board[row][col] == 'T' && board[row+1][col+1] == 'T' ||     
        board[row][col] == 'T' && board[row-1][col-1] == 'T') {
        return true;
    } else if (board[row][col] == 'T' && board[row-1][col+1] == 'T' || 
        board[row][col] == 'T' && board[row+1][col-1] == 'T') {     
        return true;
    }

    return false;
}

这可能与您尝试的约1/10(或更少)代码有关(更重要的是没有错误):

Concrete <- read.table(text="TStrength accelerator plasticiser
1   3.417543           1           1
2   2.887113           1           2
3   3.600988           1           3
4   3.702631           1           4
5   3.686944           1           5
6   3.699785           1           1
7   3.112972           1           2
8   3.918160           1           3
9   3.600538           1           4
10  2.748832           1           5
11  3.404498           1           1
12  3.735437           1           2
13  3.347577           1           3
14  3.101556           1           4
15  3.527621           1           5
16  3.856831           1           1
17  3.492118           1           2
18  3.928343           1           3
19  3.511689           1           4
20  3.371985           1           5
21  3.069794           2           1
22  3.168010           2           2
23  3.316657           2           3
24  3.455162           2           4
25  2.818250           2           5
26  4.054507           2           1
27  3.065984           2           2
28  3.201351           2           3
29  3.417554           2           4
30  3.364320           2           5
31  3.218677           2           1
32  2.647151           2           2
33  3.222705           2           3
34  3.145210           2           4
35  3.636642           2           5
36  3.317620           2           1
37  3.645922           2           2
38  2.556071           2           3
39  3.177663           2           4
40  3.014374           2           5
41  3.838183           3           1
42  4.155951           3           2
43  3.886330           3           3
44  3.723898           3           4
45  4.425442           3           5
46  3.738460           3           1
47  3.217834           3           2
48  3.942241           3           3
49  3.699851           3           4
50  3.797089           3           5
51  3.652456           3           1
52  4.851609           3           2
53  3.359099           3           3
54  4.089559           3           4
55  4.282991           3           5
56  3.803784           3           1
57  3.519551           3           2
58  3.935084           3           3
59  3.890324           3           4
60  4.611936           3           5
61  3.343098           4           1
62  3.713952           4           2
63  3.629883           4           3
64  3.082509           4           4
65  3.346548           4           5
66  3.277845           4           1
67  3.509506           4           2
68  3.490567           4           3
69  3.235009           4           4
70  3.970925           4           5
71  3.504646           4           1
72  3.270798           4           2
73  3.547298           4           3
74  3.278489           4           4
75  3.322743           4           5
76  2.975010           4           1
77  3.384996           4           2
78  3.399486           4           3
79  3.703567           4           4
80  3.214973           4           5", header=TRUE)

重要的是,您忘记在> means.by.type <- with( Concrete, tapply(TStrength, list( acc=accelerator, plas=plasticiser), FUN=mean)) > means.by.type plas acc 1 2 3 4 5 1 3.594664 3.306910 3.698767 3.479103 3.333845 2 3.415150 3.131767 3.074196 3.298897 3.208397 3 3.758221 3.936236 3.780689 3.850908 4.279364 4 3.275150 3.469813 3.516808 3.324893 3.463797 上提供strdput,因此无法确定您的问题是数据准备还是编码。