0

I am trying to do some analysis of teaching evaluations from multiple sessions with different teachers. Each student's evaluation is stores as a .csv file (although they are tab separated).

My usual approach of combining the csv files into a single data frame wont work because each file has a different number of columns and have the teacher's name in the column name. So there is a mismatch between names and dimensions. I skipped the first line and set header to FALSE, but the different number of columns still throws an error.

So, I read each .csv file into it's own data frame with the same name as the file with:

for(i in file_names){
        assign(i, read.csv(i, sep="\t", fileEncoding = "utf-16"))
}

Is there a way to use dplyr in a loop to rbind columns with specific text in the name across all data frames (50 data frames for this first round)?

Specifically I want to pull the Created.At variable and variables containing ..."Over.all.rating.for.teacher".

Edit to add sample data:

Data1 <- dput(Data1)
structure(list(Created.At = structure(c(3L, 4L, 5L, 6L, 7L, 8L, 
9L, 1L, 2L, 10L), .Label = c("2016/01/19 10:16:08 PM", "2016/01/19 11:08:58 PM", 
"2016/01/19 3:36:24 PM", "2016/01/19 4:06:32 PM", "2016/01/19 4:08:52 PM", 
"2016/01/19 4:40:26 PM", "2016/01/19 6:38:57 PM", "2016/01/19 8:18:20 PM", 
"2016/01/19 8:58:38 PM", "2016/01/20 8:16:28 PM"), class = "factor"), 
    Please.rate.teacher..John.Doe...Skills.of.interaction.and.rapport.with.learners = c(4L, 
    5L, 4L, NA, 4L, 5L, 5L, 4L, 4L, 3L), Please.rate.teacher..John.Doe...Clearly.communicated.goals.outcomes.for.the.session = c(4L, 
    5L, 4L, NA, 4L, 5L, 4L, 5L, 4L, 4L), Please.rate.teacher..John.Doe...Knowledge.of.subject.was.clearly.demonstrated = c(5L, 
    5L, 4L, NA, 4L, 5L, 5L, 5L, 4L, 3L), Please.rate.teacher..John.Doe...Conveys.the.significance.of.the.information = c(4L, 
    5L, 3L, NA, 4L, 5L, 5L, 4L, 4L, 3L), Please.rate.teacher..John.Doe...Class.preparation.materials....referred.to.or.used = c(NA, 
    NA, 4L, 5L, 4L, 5L, 5L, NA, 4L, 2L), Please.rate.teacher..John.Doe...Teaching.methods.facilitated.achievement.of.goals.for.session = c(4L, 
    5L, 4L, NA, 4L, 5L, 5L, 5L, 5L, 3L), Please.rate.teacher..John.Doe...Uses.time.effectively = c(5L, 
    5L, 4L, NA, 4L, 4L, 4L, 5L, 3L, 3L), Please.rate.teacher..John.Doe...Compared.to.other.teachers..this.one.is... = c(4L, 
    5L, 4L, NA, 4L, 5L, 5L, 4L, 4L, 4L), Please.rate.teacher..John.Doe...Over.all.rating.for.teacher = c(4L, 
    5L, 4L, 5L, 4L, 5L, 5L, 4L, 4L, 4L), Please.rate.teacher..Jane.Doe....Skills.of.interaction.and.rapport.with.learners = c(4L, 
    4L, 4L, 5L, 4L, 5L, 5L, 4L, 5L, 3L), Please.rate.teacher..Jane.Doe...Clearly.communicated.goals.outcomes.for.the.session = c(4L, 
    5L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 3L), Please.rate.teacher..Jane.Doe....Knowledge.of.subject.was.clearly.demonstrated = c(4L, 
    4L, 3L, 4L, 4L, 5L, 5L, 4L, 5L, 4L), Please.rate.teacher..Jane.Doe....Conveys.the.significance.of.the.information = c(4L, 
    4L, 4L, 4L, 4L, 5L, 5L, 4L, 5L, 3L), Please.rate.teacher..Jane.Doe....Class.preparation.materials....referred.to.or.used = c(NA, 
    NA, 4L, NA, 4L, 5L, 5L, NA, 5L, 2L), Please.rate.teacher..Jane.Doe....Teaching.methods.facilitated.achievement.of.goals.for.session = c(4L, 
    5L, 4L, 5L, 4L, 5L, 4L, 5L, 5L, 3L), Please.rate.teacher..Jane.Doe....Uses.time.effectively = c(4L, 
    5L, 4L, 5L, 4L, 5L, 5L, 5L, 5L, 3L), Please.rate.teacher..Jane.Doe...Compared.to.other.teachers..this.one.is... = c(4L, 
    4L, 4L, 4L, 4L, 5L, 4L, 4L, 4L, 3L), Please.rate.teacher..Jane.Doe....Over.all.rating.for.teacher = c(4L, 
    4L, 4L, 4L, 4L, 5L, 5L, 4L, 4L, 3L), Please.rate.teacher..Sue.Smith....Skills.of.interaction.and.rapport.with.learners = c(4L, 
    4L, 4L, 5L, 4L, 4L, 5L, 4L, 4L, 2L), Please.rate.teacher..Sue.Smith....Clearly.communicated.goals.outcomes.for.the.session = c(4L, 
    4L, 4L, 5L, 4L, 5L, 5L, 5L, 4L, 2L), Please.rate.teacher..Sue.Smith....Knowledge.of.subject.was.clearly.demonstrated = c(4L, 
    3L, 4L, 5L, 4L, NA, 5L, 4L, 4L, 2L), Please.rate.teacher..Sue.Smith....Conveys.the.significance.of.the.information = c(3L, 
    4L, 4L, 4L, 4L, NA, 5L, 4L, 3L, 2L), Please.rate.teacher..Sue.Smith....Class.preparation.materials....referred.to.or.used = c(NA, 
    NA, 4L, 4L, 4L, NA, 5L, NA, 4L, 2L), Please.rate.teacher..Sue.Smith....Teaching.methods.facilitated.achievement.of.goals.for.session = c(4L, 
    4L, 4L, 5L, 4L, NA, 5L, 4L, 5L, 2L), Please.rate.teacher..Sue.Smith....Uses.time.effectively = c(3L, 
    4L, 4L, 5L, 4L, NA, 5L, 5L, 4L, 2L), Please.rate.teacher..Sue.Smith....Compared.to.other.teachers..this.one.is... = c(4L, 
    3L, 4L, 5L, 4L, 4L, 5L, 4L, 4L, 3L), Please.rate.teacher..Sue.Smith....Over.all.rating.for.teacher = c(NA, 
    3L, 4L, 5L, 4L, 5L, 5L, 4L, 4L, 2L), Please.rate.the.following.....I.feel.that.I.achieved.the.learning.objectives.for.today.s.sessions = c(4L, 
    5L, 4L, 5L, 4L, 5L, 5L, 5L, 5L, 4L), Please.rate.the.following.....The.session.promoted.ideas.for.dissemination.of.concepts.in.my.home.department.or.other.areas = c(3L, 
    5L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 4L), Please.rate.the.following.....I.feel.prepared.to.disseminate.these.ideas.concepts.in.my.home.department = c(3L, 
    4L, 4L, 5L, 3L, 5L, 3L, 4L, 4L, 2L), Please.rate.the.following.....I.can.see.myself.making.use.of.handouts.and.follow.up.material.references.that.were.provided.in.class.today = c(NA, 
    4L, 4L, 4L, 3L, 5L, 3L, NA, 5L, 2L), Overall.I.found.this.session.to.be... = c(4L, 
    5L, 4L, 5L, 4L, 5L, 5L, 5L, 5L, 3L)), .Names = c("Created.At", 
"Please.rate.teacher..John.Doe...Skills.of.interaction.and.rapport.with.learners", 
"Please.rate.teacher..John.Doe...Clearly.communicated.goals.outcomes.for.the.session", 
"Please.rate.teacher..John.Doe...Knowledge.of.subject.was.clearly.demonstrated", 
"Please.rate.teacher..John.Doe...Conveys.the.significance.of.the.information", 
"Please.rate.teacher..John.Doe...Class.preparation.materials....referred.to.or.used", 
"Please.rate.teacher..John.Doe...Teaching.methods.facilitated.achievement.of.goals.for.session", 
"Please.rate.teacher..John.Doe...Uses.time.effectively", "Please.rate.teacher..John.Doe...Compared.to.other.teachers..this.one.is...", 
"Please.rate.teacher..John.Doe...Over.all.rating.for.teacher", 
"Please.rate.teacher..Jane.Doe....Skills.of.interaction.and.rapport.with.learners", 
"Please.rate.teacher..Jane.Doe...Clearly.communicated.goals.outcomes.for.the.session", 
"Please.rate.teacher..Jane.Doe....Knowledge.of.subject.was.clearly.demonstrated", 
"Please.rate.teacher..Jane.Doe....Conveys.the.significance.of.the.information", 
"Please.rate.teacher..Jane.Doe....Class.preparation.materials....referred.to.or.used", 
"Please.rate.teacher..Jane.Doe....Teaching.methods.facilitated.achievement.of.goals.for.session", 
"Please.rate.teacher..Jane.Doe....Uses.time.effectively", "Please.rate.teacher..Jane.Doe...Compared.to.other.teachers..this.one.is...", 
"Please.rate.teacher..Jane.Doe....Over.all.rating.for.teacher", 
"Please.rate.teacher..Sue.Smith....Skills.of.interaction.and.rapport.with.learners", 
"Please.rate.teacher..Sue.Smith....Clearly.communicated.goals.outcomes.for.the.session", 
"Please.rate.teacher..Sue.Smith....Knowledge.of.subject.was.clearly.demonstrated", 
"Please.rate.teacher..Sue.Smith....Conveys.the.significance.of.the.information", 
"Please.rate.teacher..Sue.Smith....Class.preparation.materials....referred.to.or.used", 
"Please.rate.teacher..Sue.Smith....Teaching.methods.facilitated.achievement.of.goals.for.session", 
"Please.rate.teacher..Sue.Smith....Uses.time.effectively", "Please.rate.teacher..Sue.Smith....Compared.to.other.teachers..this.one.is...", 
"Please.rate.teacher..Sue.Smith....Over.all.rating.for.teacher", 
"Please.rate.the.following.....I.feel.that.I.achieved.the.learning.objectives.for.today.s.sessions", 
"Please.rate.the.following.....The.session.promoted.ideas.for.dissemination.of.concepts.in.my.home.department.or.other.areas", 
"Please.rate.the.following.....I.feel.prepared.to.disseminate.these.ideas.concepts.in.my.home.department", 
"Please.rate.the.following.....I.can.see.myself.making.use.of.handouts.and.follow.up.material.references.that.were.provided.in.class.today", 
"Overall.I.found.this.session.to.be..."), class = "data.frame", row.names = c(NA, 
-10L))

Data2 <- dput(Data2)
structure(list(Created.At = structure(c(1L, 2L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L), .Label = c("2016/09/13 4:28:24 PM", 
"2016/09/13 4:29:11 PM", "2016/09/13 4:29:23 PM", "2016/09/13 4:29:29 PM", 
"2016/09/13 4:29:34 PM", "2016/09/13 4:29:37 PM", "2016/09/13 4:29:40 PM", 
"2016/09/13 4:29:41 PM", "2016/09/13 4:29:49 PM", "2016/09/13 4:30:19 PM", 
"2016/09/13 4:32:42 PM", "2016/09/13 4:35:50 PM", "2016/09/13 4:41:46 PM", 
"2016/09/13 9:41:27 PM", "2016/09/26 10:53:28 PM", "2016/10/11 10:30:34 PM"
), class = "factor"), Please.rate.teacher..Foo.Bar...Skills.of.interaction.and.rapport.with.learners = c(5L, 
5L, 4L, 4L, 4L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 5L, 4L, 5L, 5L, 5L
), Please.rate.teacher..Foo.Bar...Clearly.communicated.goals.outcomes.for.the.session = c(5L, 
5L, 4L, 4L, 4L, 5L, 5L, 4L, 5L, 5L, 4L, 4L, 5L, 4L, 4L, 5L, 5L
), Please.rate.teacher..Foo.Bar...Knowledge.of.subject.was.clearly.demonstrated = c(5L, 
3L, 4L, 4L, 5L, 5L, 4L, 3L, 4L, 5L, 4L, 4L, 4L, 5L, 4L, 5L, 5L
), Please.rate.teacher..Foo.Bar...Conveys.the.significance.of.the.information = c(5L, 
5L, 4L, 4L, 4L, 5L, 5L, 3L, 4L, 5L, 4L, 4L, 5L, 4L, 4L, 5L, 5L
), Please.rate.teacher..Foo.Bar...Class.preparation.materials....referred.to.or.used = c(5L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 4L, 5L, 5L, 4L, 5L, 4L, 5L, 5L, 5L
), Please.rate.teacher..Foo.Bar...Teaching.methods.facilitated.achievement.of.goals.for.session = c(5L, 
5L, 5L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 5L, 5L, 5L
), Please.rate.teacher..Foo.Bar...Uses.time.effectively = c(5L, 
5L, 4L, 4L, 5L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L
), Please.rate.teacher..Foo.Bar...Compared.to.other.teachers..this.one.is... = c(5L, 
5L, 4L, 4L, 4L, 5L, 5L, 4L, 4L, 5L, 4L, 4L, 5L, 4L, 4L, 5L, 5L
), Please.rate.teacher..Foo.Bar...Over.all.rating.for.teacher = c(5L, 
4L, 4L, 4L, 4L, 5L, 5L, 4L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 5L
), Please.rate.the.following.....I.feel.that.I.achieved.the.learning.objectives.for.today.s.sessions = c(4L, 
4L, NA, 4L, 5L, 5L, 4L, 4L, 5L, 5L, 5L, 4L, 4L, 5L, 5L, 4L, 4L
), Please.rate.the.following.....The.session.promoted.ideas.for.dissemination.of.concepts.in.my.home.department.or.other.areas = c(5L, 
4L, NA, NA, 4L, 5L, 4L, 4L, 5L, 4L, 5L, 4L, 4L, 4L, 5L, 5L, 5L
), Please.rate.the.following.....I.feel.prepared.to.disseminate.these.ideas.concepts.in.my.home.department = c(4L, 
3L, NA, 4L, 4L, 5L, 4L, 3L, 4L, 4L, 5L, 4L, 3L, 4L, 5L, 4L, 4L
), Please.rate.the.following.....I.can.see.myself.making.use.of.handouts.and.follow.up.material.references.that.were.provided.in.class.today = c(5L, 
4L, NA, NA, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 5L, 5L, 5L, 5L
), Overall.I.found.this.session.to.be... = c(5L, 4L, 4L, 3L, 
4L, 5L, 4L, 4L, 4L, 4L, 5L, 4L, 4L, 5L, 5L, 5L, 5L)), .Names = c("Created.At", 
"Please.rate.teacher..Foo.Bar...Skills.of.interaction.and.rapport.with.learners", 
"Please.rate.teacher..Foo.Bar...Clearly.communicated.goals.outcomes.for.the.session", 
"Please.rate.teacher..Foo.Bar...Knowledge.of.subject.was.clearly.demonstrated", 
"Please.rate.teacher..Foo.Bar...Conveys.the.significance.of.the.information", 
"Please.rate.teacher..Foo.Bar...Class.preparation.materials....referred.to.or.used", 
"Please.rate.teacher..Foo.Bar...Teaching.methods.facilitated.achievement.of.goals.for.session", 
"Please.rate.teacher..Foo.Bar...Uses.time.effectively", "Please.rate.teacher..Foo.Bar...Compared.to.other.teachers..this.one.is...", 
"Please.rate.teacher..Foo.Bar...Over.all.rating.for.teacher", 
"Please.rate.the.following.....I.feel.that.I.achieved.the.learning.objectives.for.today.s.sessions", 
"Please.rate.the.following.....The.session.promoted.ideas.for.dissemination.of.concepts.in.my.home.department.or.other.areas", 
"Please.rate.the.following.....I.feel.prepared.to.disseminate.these.ideas.concepts.in.my.home.department", 
"Please.rate.the.following.....I.can.see.myself.making.use.of.handouts.and.follow.up.material.references.that.were.provided.in.class.today", 
"Overall.I.found.this.session.to.be..."), class = "data.frame", row.names = c(NA, 
-17L))

Data1 has 3 teachers in the session, Data2 has only a single teacher. I think to make sense of the data, and match with other demographic info, I'll need to create a variable for "teacher name".

Edit to show desired output:

               Created.At Rating                                                           Var
1   2016/01/19 3:36:24 PM      4   Please rate teacher: John Doe | Over all rating for teacher
2   2016/01/19 4:06:32 PM      5   Please rate teacher: John Doe | Over all rating for teacher
3   2016/01/19 4:08:52 PM      4   Please rate teacher: John Doe | Over all rating for teacher
4   2016/01/19 4:40:26 PM      5   Please rate teacher: John Doe | Over all rating for teacher
5   2016/01/19 6:38:57 PM      4   Please rate teacher: John Doe | Over all rating for teacher
6   2016/01/19 8:18:20 PM      5   Please rate teacher: John Doe | Over all rating for teacher
7   2016/01/19 8:58:38 PM      5   Please rate teacher: John Doe | Over all rating for teacher
8  2016/01/19 10:16:08 PM      4   Please rate teacher: John Doe | Over all rating for teacher
9  2016/01/19 11:08:58 PM      4   Please rate teacher: John Doe | Over all rating for teacher
10  2016/01/20 8:16:28 PM      4   Please rate teacher: John Doe | Over all rating for teacher
11  2016/01/19 3:36:24 PM      4  Please rate teacher: Jane Doe  | Over all rating for teacher
12  2016/01/19 4:06:32 PM      4  Please rate teacher: Jane Doe  | Over all rating for teacher
13  2016/01/19 4:08:52 PM      4  Please rate teacher: Jane Doe  | Over all rating for teacher
14  2016/01/19 4:40:26 PM      4  Please rate teacher: Jane Doe  | Over all rating for teacher
15  2016/01/19 6:38:57 PM      4  Please rate teacher: Jane Doe  | Over all rating for teacher
16  2016/01/19 8:18:20 PM      5  Please rate teacher: Jane Doe  | Over all rating for teacher
17  2016/01/19 8:58:38 PM      5  Please rate teacher: Jane Doe  | Over all rating for teacher
18 2016/01/19 10:16:08 PM      4  Please rate teacher: Jane Doe  | Over all rating for teacher
19 2016/01/19 11:08:58 PM      4  Please rate teacher: Jane Doe  | Over all rating for teacher
20  2016/01/20 8:16:28 PM      3  Please rate teacher: Jane Doe  | Over all rating for teacher
21  2016/01/19 3:36:24 PM     NA Please rate teacher: Sue Smith  | Over all rating for teacher
22  2016/01/19 4:06:32 PM      3 Please rate teacher: Sue Smith  | Over all rating for teacher
23  2016/01/19 4:08:52 PM      4 Please rate teacher: Sue Smith  | Over all rating for teacher
24  2016/01/19 4:40:26 PM      5 Please rate teacher: Sue Smith  | Over all rating for teacher
25  2016/01/19 6:38:57 PM      4 Please rate teacher: Sue Smith  | Over all rating for teacher
26  2016/01/19 8:18:20 PM      5 Please rate teacher: Sue Smith  | Over all rating for teacher
27  2016/01/19 8:58:38 PM      5 Please rate teacher: Sue Smith  | Over all rating for teacher
28 2016/01/19 10:16:08 PM      4 Please rate teacher: Sue Smith  | Over all rating for teacher
29 2016/01/19 11:08:58 PM      4 Please rate teacher: Sue Smith  | Over all rating for teacher
30  2016/01/20 8:16:28 PM      2 Please rate teacher: Sue Smith  | Over all rating for teacher
31  2016/09/13 4:28:24 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher
32  2016/09/13 4:29:11 PM      4    Please rate teacher: Foo Bar | Over all rating for teacher
33  2016/09/13 4:29:11 PM      4    Please rate teacher: Foo Bar | Over all rating for teacher
34  2016/09/13 4:29:23 PM      4    Please rate teacher: Foo Bar | Over all rating for teacher
35  2016/09/13 4:29:29 PM      4    Please rate teacher: Foo Bar | Over all rating for teacher
36  2016/09/13 4:29:34 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher
37  2016/09/13 4:29:37 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher
38  2016/09/13 4:29:40 PM      4    Please rate teacher: Foo Bar | Over all rating for teacher
39  2016/09/13 4:29:41 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher
40  2016/09/13 4:29:49 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher
41  2016/09/13 4:30:19 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher
42  2016/09/13 4:32:42 PM      4    Please rate teacher: Foo Bar | Over all rating for teacher
43  2016/09/13 4:35:50 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher
44  2016/09/13 4:41:46 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher
45  2016/09/13 9:41:27 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher
46 2016/09/26 10:53:28 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher
47 2016/10/11 10:30:34 PM      5    Please rate teacher: Foo Bar | Over all rating for teacher

Ideal would be something like:

               Created.At Overall.Rating   Teacher
1   2016/01/19 3:36:24 PM              4  John Doe
2   2016/01/19 4:06:32 PM              5  John Doe
3   2016/01/19 4:08:52 PM              4  John Doe
4   2016/01/19 4:40:26 PM              5  John Doe
5   2016/01/19 6:38:57 PM              4  John Doe
6   2016/01/19 8:18:20 PM              5  John Doe
7   2016/01/19 8:58:38 PM              5  John Doe
8  2016/01/19 10:16:08 PM              4  John Doe
9  2016/01/19 11:08:58 PM              4  John Doe
10  2016/01/20 8:16:28 PM              4  John Doe
11  2016/01/19 3:36:24 PM              4  Jane Doe
12  2016/01/19 4:06:32 PM              4  Jane Doe
13  2016/01/19 4:08:52 PM              4  Jane Doe
14  2016/01/19 4:40:26 PM              4  Jane Doe
15  2016/01/19 6:38:57 PM              4  Jane Doe
16  2016/01/19 8:18:20 PM              5  Jane Doe
17  2016/01/19 8:58:38 PM              5  Jane Doe
18 2016/01/19 10:16:08 PM              4  Jane Doe
19 2016/01/19 11:08:58 PM              4  Jane Doe
20  2016/01/20 8:16:28 PM              3  Jane Doe
21  2016/01/19 3:36:24 PM             NA Sue Smith
22  2016/01/19 4:06:32 PM              3 Sue Smith
23  2016/01/19 4:08:52 PM              4 Sue Smith
24  2016/01/19 4:40:26 PM              5 Sue Smith
25  2016/01/19 6:38:57 PM              4 Sue Smith
26  2016/01/19 8:18:20 PM              5 Sue Smith
27  2016/01/19 8:58:38 PM              5 Sue Smith
28 2016/01/19 10:16:08 PM              4 Sue Smith
29 2016/01/19 11:08:58 PM              4 Sue Smith
30  2016/01/20 8:16:28 PM              2 Sue Smith
31  2016/09/13 4:28:24 PM              5   Foo Bar
32  2016/09/13 4:29:11 PM              4   Foo Bar
33  2016/09/13 4:29:11 PM              4   Foo Bar
34  2016/09/13 4:29:23 PM              4   Foo Bar
35  2016/09/13 4:29:29 PM              4   Foo Bar
36  2016/09/13 4:29:34 PM              5   Foo Bar
37  2016/09/13 4:29:37 PM              5   Foo Bar
38  2016/09/13 4:29:40 PM              4   Foo Bar
39  2016/09/13 4:29:41 PM              5   Foo Bar
40  2016/09/13 4:29:49 PM              5   Foo Bar
41  2016/09/13 4:30:19 PM              5   Foo Bar
42  2016/09/13 4:32:42 PM              4   Foo Bar
43  2016/09/13 4:35:50 PM              5   Foo Bar
44  2016/09/13 4:41:46 PM              5   Foo Bar
45  2016/09/13 9:41:27 PM              5   Foo Bar
46 2016/09/26 10:53:28 PM              5   Foo Bar
47 2016/10/11 10:30:34 PM              5   Foo Bar
JLC
  • 661
  • 7
  • 16
  • Please have a look at https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. It would be easier if reproducible example is provided wtih question. – MKR Mar 14 '18 at 21:58
  • 1
    I have added some example data – JLC Mar 14 '18 at 22:16

4 Answers4

1

Based on some of the other answers and some digging and hacking I tried the following and it seems to work:

library(tidyr)

# Read in file path to .csv subject files
Filepath <- dirname(file.choose()) # Choose a file in the directory
                                   #  with all the .csv files

# Get a list of all files in the directory
file_names <- dir(Filepath, full.names = TRUE) 

# Function to read .csv files listed from directory
read_data <- function(file_names) {
        read.csv(file_names, sep = "\t", fileEncoding = "utf-16",
             check.names = FALSE, stringsAsFactors = FALSE) 
}

# Create a list of data frames from .csv files
data_list <- lapply(file_names, read_data)

# Create a wide data from of all rows from variables
Data_Wide <- lapply(data_list, select, `Created At`,
                    contains("Over all rating for teacher")) %>% 
             bind_rows()

# Gather to long data
All_Data <- gather(Data_Wide, Teacher, Overall_Rating, -`Created At`,na.rm = T) %>%
    mutate(Teacher = gsub("Please rate teacher: |
     [|] Over all rating for teacher|
       [(]Scholarship[)]|[(]Teaching Excellence[)]", "", Teacher ),
       Teacher = trimws(Teacher), Teacher = tolower(Teacher), 
       Teacher = tools::toTitleCase(Teacher))

If anyone has any more efficient or cleaner approaches, please post :)

JLC
  • 661
  • 7
  • 16
  • You have arrived at a good solution. Couple of comments: 1) You can choose directory directly as `Filepath <- choose.dir()`; 2) Argument `full.names = TRUE` can be added to `dir` to remove dependency from working directory. 3) `lapply` could have been called as: `lapply(lst, select, "Created.At", contains("Over.all.rating.for.teacher") )` 4) You can combine all `mutate` in one. Good work. – MKR Mar 15 '18 at 07:42
  • You can even mention use of `tidyr`. – MKR Mar 15 '18 at 07:43
  • Thanks for the suggestions! The use of `Filepath <- dirname(file.choose())` is a workaround because I'm on linux; `choose.dir()` is a Windows-only function (unfortunately). Your other suggestions clean things up nicely :) Thanks!! – JLC Mar 15 '18 at 13:45
0

You could try storing them in a list, rather than assigning each frame into a global variable.

library(dplyr)
read_data <- function(files) {
  read.csv(files) %>% 
    dplyr::mutate(id_col = files)
}
filenames <- list.files(pattern = ".csv")

mydata <- lapply(files, read_data)

That gets you a list with all the dataframes. Then select the columns you want

new_data <- lapply(mydata, function(x){
  dplyr::select(x, Created.At, id_col, 
contains("Over.all.rating.for.teacher"))
  return(x)
})

Note that I cant test this exactly due to the lack of a reproducible example, but this should set you on the right track

Conor Neilson
  • 1,026
  • 1
  • 11
  • 27
  • Thanks! I have added some example data to the original question – JLC Mar 14 '18 at 22:16
  • Unfortunately, mydata and new_data are both lists that contain all of the variables. The number of variables actually increases to 55 from 36 or 45 (depending on the number of teachers) – JLC Mar 14 '18 at 22:50
0

Here's a shorter way to do it. You can get list of dataframes in your current environment using ls().

In the example below, I select column name from two data frames. This is similar to the problem you are solving I guess:

library(purrr)

# sample dataframes
df1 <- data.frame(name = c('a','b','c'), val1 = c(1,2,3))
df2 <- data.frame(name = c('d','e','f'), val2 = c(1,2,3),val3 = c(7,8,9))


# create a list of dataframes
list_of_dataframes <- list(df1, df2)

# select columns and create final dataframe
output <- do.call(rbind, map(list_of_dataframes,`[` ,'name')) # instead of 'name' here you can specify a vector c('Created.at','another_column','another_column')

print(output)

  name
1    a
2    b
3    c
4    d
5    e
6    f
YOLO
  • 20,181
  • 5
  • 20
  • 40
0

One option could be to use dplyr::select_at and dplyr::bind_rows. The select_at will be used to get only columns containing Over.all.rating.for.teacher or Created.At

library(dplyr)


res <- Data1 %>% 
select_at(vars(c("Created.At"),grep("Over.all.rating.for.teacher", 
names(Data1), value = TRUE))) %>%
  bind_rows(Data2 %>% 
      select_at(vars(c("Created.At"),grep("Over.all.rating.for.teacher", 
      names(Data2), value = TRUE))))


str(res)
'data.frame':   27 obs. of  5 variables:
 $ Created.At                                                   : chr  "2016/01/19 3:36:24 PM" "2016/01/19 4:06:32 PM" "2016/01/19 4:08:52 PM" "2016/01/19 4:40:26 PM" ...
 $ Please.rate.teacher..John.Doe...Over.all.rating.for.teacher  : int  4 5 4 5 4 5 5 4 4 4 ...
 $ Please.rate.teacher..Jane.Doe....Over.all.rating.for.teacher : int  4 4 4 4 4 5 5 4 4 3 ...
 $ Please.rate.teacher..Sue.Smith....Over.all.rating.for.teacher: int  NA 3 4 5 4 5 5 4 4 2 ...
 $ Please.rate.teacher..Foo.Bar...Over.all.rating.for.teacher   : int  NA NA NA NA NA NA NA NA NA NA ...

Note: The data shared by OP contains strings as Factor hence above solution may give warning. It would be better to convert columns to character before operating on data frames.

MKR
  • 19,739
  • 4
  • 23
  • 33
  • Is there a way to script this to loop over all ~50 data frames/files without having to name them individually? – JLC Mar 14 '18 at 23:14
  • @JLC Yes. That is possible as well. Hint: You can create a list containing data.frames and then run `select_at` one by one and rbind. I can work-out on such answer tomorrow. – MKR Mar 14 '18 at 23:17
  • Thanks for the hint. So far, I've been able to create a list of all the data frames in the workspace with `df_list <- lapply(ls(), function(x) if (class(get(x)) == "data.frame") get(x))` But I can't quite figure out how to step through each element with `select_at` and `rbind` – JLC Mar 15 '18 at 01:42
  • @JLC Happy to see you have arrived at a good solution. Well done. – MKR Mar 15 '18 at 07:37