MM1
MM1

Reputation: 506

Replacing a NA title/names of dataframes inside a list in r

I am having trouble reproducing this error with an example list of dataframes, so bear with me...

Say you have the list of dataframes here:

#Example data frame columns
Image <- c("001", "001", "001", "001", "002", "002", "002", "002", "003", "003", "003", "003")
Size <- c("Big", "Small", "Medium", "Tiny", "Big", "Small", "Medium", "Tiny", "Big", "Small", "Medium", "Tiny")
n <- c(111778, 56, 7099, 3, 3682081, 88, 9078, 7, 198346, 422, 30077, 8)

#make example data frame
data <- data.frame(Image, Size, n)

#Split dataframe into a list of dataframes
df <- split(data, f = data$Image) 
df

output:

$`001`
  Image   Size      n
1   001    Big 111778
2   001  Small     56
3   001 Medium   7099
4   001   Tiny      3

$`002`
  Image   Size       n
5   002    Big 3682081
6   002  Small      88
7   002 Medium    9078
8   002   Tiny       7

etc

See how the titles of the dataframes are $001 and $002 etc.

Problem

My own list of dataframes are named as NA.

How do I

  1. Rename the list of dataframes, based on row names.

E.g. If I have a column in every dataframe called Label, with repetitions of

$`NA`
Label   Area
00017    ...
00017
00017
etc

#New dataframe
$`NA`
Label
00021
00021
00021

How do I replace the NA with the corresponding label? i.e. 00017 and 00021 etc.

I cannot go back and use the split function to rename, as some part of my data wrangling renames them NA. So it needs to be after all of this has occurred.

Upvotes: 0

Views: 46

Answers (2)

MM1
MM1

Reputation: 506

I figured it out. I just merged the list of dataframes into one using

df_merged <- as.data.frame(data.table::rbindlist(df_original_list))

And then using the split function based on the label.

df <- split(df_merged, f = df_merged$Label) 

To rename the elements inside the list.

Upvotes: 1

John Polo
John Polo

Reputation: 802

You called them rownames, but they seem more like just values in the Label column. Regardless, this should work.

> names(df) <- "NA"
> df
$`NA`
  Image   Size      n
1   001    Big 111778
2   001  Small     56
3   001 Medium   7099
4   001   Tiny      3

$<NA>
  Image   Size       n
5   002    Big 3682081
6   002  Small      88
7   002 Medium    9078
8   002   Tiny       7

$<NA>
   Image   Size      n
9    003    Big 198346
10   003  Small    422
11   003 Medium  30077
12   003   Tiny      8

> for(i in 1:length(df)) names(df)[i] = df[[i]][1,1]

> df
$`001`
  Image   Size      n
1   001    Big 111778
2   001  Small     56
3   001 Medium   7099
4   001   Tiny      3

$`002`
  Image   Size       n
5   002    Big 3682081
6   002  Small      88
7   002 Medium    9078
8   002   Tiny       7

$`003`
   Image   Size      n
9    003    Big 198346
10   003  Small    422
11   003 Medium  30077
12   003   Tiny      8

Upvotes: 1

Related Questions