[Solved] Processing the data in a matrix in r [closed]


NOTE: Your question as it stands is not appropriate for this site. You should include a minimum reproducible example. Include code that you’ve tried and where it’s causing you trouble. At this point, you’re likely to continue getting down votes, and your question may potentially get closed. Be aware of that. Please visit the help center to improve your question.

That said, you’re new to R and I will try to help you get started on this. But please consult the help center to get started on Stack Overflow.


Let’s assume your matrix is stored with the name mat. First convert it to a data frame.

df <- as.data.frame(mat)

This will have columns called V1 and V2. You can rename them if you want to using colnames(df) <- c("whatever", "something"). I’ll proceed assuming you’ve kept the original names.

Now get the file name from the second column.

df$file.name <- with(df, tail(strsplit(V2, "https://stackoverflow.com/")[[1]], n=1))

There’s a lot going on here, so let’s look at it one step at a time.

> strsplit("/home/psycodelic/Desktop/r_source//AAPL.csv", "https://stackoverflow.com/")
[[1]]
[1] ""  "home"  "psycodelic"  "Desktop"  "r_source"  ""  "AAPL.csv"

This is a list with one element. We can get that single element using [[1]]. That element contains a vector of strings which result from splitting the input at the / character.

You only want the file name, which is the last part of the file path. Thus we can get the last element of the vector using tail(..., n=1).

with(df, ...) ensures that V2 is interpreted in the context of the data frame df.

So at this point df has three columns: the two originals, plus one containing the file names. The next step is to remove the NA values. We can do this using the na.omit() function.

df <- na.omit(df)

You can blindly convert the floats in V1 to integers using as.integer().

df$V1 <- as.numeric(df$V1)

Getting close! Sort ascending like so:

df <- df[with(df, order(V1)), ]

Now create a new data frame and print it.

(df2 <- head(df, n=10))

Surrounding an assignment in parentheses will both print it and assign it.

solved Processing the data in a matrix in r [closed]