0

I have one input file.

cat file

 ABC   2 3 4
 DEF   3 4 5 
 FRD   3 5 6

 input<-read.table(file)

I want to load this in dataframe where names are key and values are vector of numbers in a row so that i can get the values.

How can i store the data in dataframe dynamically?

2 Answers 2

1

If you stored those values with the first column as rownames you could access using this formalism:

 dfrm["ABC", ]   # result  c(2,3,4)

 dfrm <- read.table(text="V1 V2 V3
  ABC   2 3 4
  DEF   3 4 5 
  FRD   3 5 6",  header=TRUE )
dfrm["ABC" , ]
#     V1 V2 V3
# ABC  2  3  4
Sign up to request clarification or add additional context in comments.

Comments

1

You are looking for a list I guess:

df <- structure(list(V1 = structure(1:3, 
      .Label = c("ABC", "DEF", "FRD"), class = "factor"), 
      V2 = c(2L, 3L, 3L), V3 = 3:5, V4 = 4:6), 
      .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", 
      row.names = c(NA, -3L))

df.l <- as.list(df[,2:4])
names(df.l) <- df[,1]

# $ABC
# [1] 2 3 3

# $DEF
# [1] 3 4 5

# $FRD
# [1] 4 5 6

Now, you can access these vectors by doing: df.l[["ABC"]]

Alternatively, if you would want to keep it as a data.frame, you should make the first column of your data.frame as it's column names:

df.o <- df[,2:4]
names(df.o) <- df[,1]

#   ABC DEF FRD
# 1   2   3   4
# 2   3   4   5
# 3   3   5   6

Now, you can access it similar to accessing a list (a data.frame is internally a list as well).

> df.o[["ABC"]] # or equivalently as df$ABC
# [1] 2 3 3

Comments

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.