1

I want all my rows to be converted to their own columns so everything will be in one row in the end. This is how my dataframe looks now:

   track                    time UTM_WGS84.Longitude UTM_WGS84.Latitude
1       1 2015-10-14 23:59:55.711            5.481687           51.43635
2       1 2015-10-14 23:59:55.717            5.481689           51.43635
3       1 2015-10-14 23:59:55.723            5.481689           51.43635
4       1 2015-10-14 23:59:55.730            5.481690           51.43635
5       1 2015-10-14 23:59:55.763            5.481691           51.43635
6       1 2015-10-14 23:59:55.804            5.481691           51.43635
7       1 2015-10-14 23:59:55.840            5.481692           51.43635
8       2 2015-10-14 23:59:55.882            5.481692           51.43635
9       2 2015-10-14 23:59:56.031            5.481693           51.43635
10      2 2015-10-14 23:59:56.041            5.481693           51.43635
11      2 2015-10-14 23:59:56.047            5.481693           51.43635
12      2 2015-10-14 23:59:56.053            5.481694           51.43635
13      3 2015-10-14 23:59:56.081            5.481695           51.43635
14      3 2015-10-14 23:59:56.121            5.481695           51.43635
15      3 2015-10-14 23:59:56.165            5.481695           51.43635

I'm not sure if this is doable but this is what I want the columns to look like:

time1.1 - UTM_WGS84.Longitude1.1 - UTM_WGS84.Latitude1.1 -  time1.2 - UTM_WGS84.Longitude1.2 - UTM_WGS84.Latitude1.2 - time2.1 -  UTM_WGS84.Longitude2.1 - UTM_WGS84.Latitude2.1

Where the first number is the track and the second number is the row of that track. I know it's a weird request but I'm trying to find a way to give all this data to a machine learning method to recognize walking patterns.

Any ideas on how to do this?

2
  • 2
    Please provide the data in an easy-to-paste form and possibly the way the expected result is suppose to look like. Commented Nov 2, 2016 at 11:13
  • 4
    You have a strange machine learning method. Commented Nov 2, 2016 at 11:21

2 Answers 2

1

Check this demo:

> df <- data.frame(track = c(2,1,5), A = c(1,2,3), B = c(100, 200, 300))
> df
  track A   B
1     2 1 100
2     1 2 200
3     5 3 300

As you wish, one row in the end, meaning combine all rows to one list ? So:

> ls <- unlist(as.list(t(df)))
> ls
[1]   2   1 100   1   2 200   5   3 300

But how to deal with column names ? A dumb approach may work out:

> row.num <- row.names(df)
> row.num
[1] "1" "2" "3"
> column.name <- colnames(df)
> column.name
[1] "track" "A"     "B"    
> track <- as.character(df$track)
> track
[1] "2" "1" "5"

Then get new column names:

> new.column.names <- c()
> for(i in 1:length(track)) {
     for(k in 1:length(column.name)) {
         str <- paste(column.name[k], track[i], sep = "")
         str <- paste(str, row.num[i], sep = ".")
         new.column.names <- c(new.column.names, str)
         }
     }
> new.column.names
[1] "track2.1" "A2.1"     "B2.1"     "track1.2" "A1.2"     "B1.2"     "track5.3"
[8] "A5.3"     "B5.3"

At last, assign ls and new.column.names to your new data frame:

> new.df <- t(data.frame(ls))
> colnames(new.df) <- new.column.names
> rownames(new.df) <- 1

Result:

> new.df
  track2.1 A2.1 B2.1 track1.2 A1.2 B1.2 track5.3 A5.3 B5.3
1        2    1  100        1    2  200        5    3  300
Sign up to request clarification or add additional context in comments.

Comments

0

See if this is what you want:

df=matrix(1:12,ncol=3)
df
#     [,1] [,2] [,3]
#[1,]    1    5    9
#[2,]    2    6   10
#[3,]    3    7   11
#[4,]    4    8   12
c(t(df))
# [1]  1  5  9  2  6 10  3  7 11  4  8 12

As data frame

data.frame(t(c(t(df))))
#  X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12
#1  1  5  9  2  6 10  3  7 11   4   8  12

1 Comment

Thank you, it's a good start but this is a list and what I want is a dataframe. Is it possible for all these numbers to have their own column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.